Selenium2+python automation 15-select drop-down box

Foreword
Recently, due to work reasons, the update has been a little slow. Today, I finally took a little time to continue to update the selenium series. The learning script cannot be stopped. I hope my friends can support it more.

This article takes Baidu to set the drop-down option box as an example, and introduces the operation methods related to the select drop-down box in detail.

1. Recognize select
    1. Open Baidu-Settings-Search Settings interface, as shown in the following figure

    2. The position pointed by the arrow is the select option box, open the page element positioning, the red box area below, you can see the select tag attribute:
<select id="nr" name="NR">
    3. There are three options
<option selected ="" value="10">10 items per page</option>
<option value="20">20 items per page</option>
<option value="50">50 items per page </option> option>

2. Secondary positioning
    1. There are many ways to locate the options in the select. Here is a simple method: secondary positioning

    2. Basic idea, first locate the select box, and then locate the options in the select

    3. The code is as follows

 

    4. There is another way of writing that is also possible, combining the bottom two steps into one step:

driver.find_element_by_id("nr").find_element_by_xpath("//option[ @value='50']").click()

3. Direct positioning
    1. There are many friends who say that firebug can only locate the select box and cannot locate To the options inside, in fact, the tools are not very proficient. The editor will teach you how to locate the options inside.
    2. After locating the select with direbug, check the element attribute below, and click the + sign in front of the select tag to expand the options inside.


    3. Then write xpath positioning or css yourself, and directly locate the content on the option at one time. (I won't write it by myself, look back at the previous element positioning content)

4. Select module (index)


    1. In addition to the two simple methods described above to locate the select option, selenium also provides a more advanced way of playing, importing the Select module. Directly locate by property or index.

    2. First import the select method:

from selenium.webdriver.support.select import Select

    3. Then locate and select the corresponding option (counting from 0) through the index of the select option, such as selecting the third option: select_by_index(2)

5. Select module (value)
    1. In addition to the index method in the Select module, there is also a method to locate by the value of the option. Each option has a corresponding value, such as
<select id="nr" name="NR">
<option selected="" value="10">display 10 items per page</option>

<option value="20">Display 20 items per page</option>

<option value="50">Display 50 items per page</option>

    2. The value corresponding to the second option is "20": select_by_value("20")

Sixth, the Select module (text)
    1. There is a more advanced function in the Select module, which can be located directly through the text content of the option.

    2. Positioning "50 items per page": select_by_visible_text("50 items per page")


7. Other methods of the Select module
    1. In addition to the three methods described above, the methods in select have more functions as follows

select_by_index(): locate by index
select_by_value(): locate by value
select_by_visible_text(): locate by text value
deselect_all(): cancel all options
deselect_by_index(): cancel the corresponding index option
deselect_by_value(): cancel the corresponding value option
deselect_by_visible_text(): Cancel the corresponding text option
first_selected_option(): return the first option
all_selected_options(): return all options

 

 

8. Arrange the code as follows:
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
url = "https:// www.baidu.com"
driver.get(url)
driver.implicitly_wait(20)
# Move the mouse to the "Settings" button
mouse = driver.find_element_by_link_text("Settings")
ActionChains(driver).move_to_element(mouse).perform()
driver .find_element_by_link_text("Search Settings").click()
# Through text:select_by_visible_text()
s = driver.find_element_by_id("nr")
Select(s).select_by_visible_text("Display 50 items per page")

# # In two steps: First locate the drop-down box, then click the option
# s = driver.find_element_by_id("nr")
# s.find_element_by_xpath("//option[@value='50']").click()

# # 另外一种写法
# driver.find_element_by_id("nr").find_element_by_xpath("//option[@value='50']").click()

# # 直接通过xpath定位
# driver.find_element_by_xpath(".//*[@id='nr']/option[2]").click()

# # 通过索引:select_by_index()
# s = driver.find_element_by_id("nr")
# Select(s).select_by_index(2)

# # 通过value:select_by_value()
# s = driver.find_element_by_id("nr")
# Select(s).select_by_value("20")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325343340&siteId=291194637