Operation of drop-down box Selenium

Operation drop-down box:

      Usually down box application scenarios: when there is the option to add drop-down box, pull-down or in two stages interlock linkage (for example: when the multi-stage pull-down interaction of selected cities and counties).

      Select the drop-down box select the label has a property, there are two attributes select and option. Such as:

 

     Its types are:

      ① radio drop-down box, an element can be selected.

      ② multi-select drop-down box, you can select multiple elements.

     Positioning method:

      ① positioned directly

      ② secondary positioning. First navigate to select box, and then navigate to select options.

      ③ introducing Select Module (recommended) - according to the attribute to locate or index.

         Select method must first import from selenium.webdriver.support.ui import Select.

         Then index: s = driver.find_element_by_id ( 'nr')

                                  Select(s).select_by_index(2)

         By the value of the positioning value: s = driver.find_element_by_id ( 'nr')

                                       Select(s).select_by_value('20')

         Positioned by text values: s = driver.find_element_by_id ( 'nr')

                                        Select(s).select_by_visible_text('宝马')

The following code is attached ms.html screenshot:

       

 

By selenium achieve:

import time 

from selenium import webdriver

from selenium.webdriver.support.ui import Select # Import Module Select

# Designated driver

driver = webdriver.Chrome(r"C:\webdriver\chromedriver.exe")

# Open URL

driver.get('file:///C:/Users\Administrator/PycharmProjects/1120/web_driver/lesson3/ms.html')

# Obtain the corresponding webElement

select = Select(driver.find_element_by_id('multi'))

# Cancel all options selected

select.deselect_all()

# The value of the positioning text

select.select_by_visible_text('雅阁')

select.select_by_visible_text('宝马 740')

# Obtain the corresponding webElement

select = Select(driver.find_element_by_id('single'))

# The value of the positioning value

select.select_by_value('male')

 

driver.quit()

Guess you like

Origin www.cnblogs.com/peipei-Study/p/11887339.html