Operation of selenium in the drop-down list

Drop-down list operations divided into two types: select, non-select

1, the non-select operating drop-down box

Consistent with the operation of the non-select drop-down list page elements to operate, to find elements positioned elements, set the wait, click on the element and so on

Next, the operation Baidu setting button, a drop-down box belonging provided

 

 

For chestnut:

from the Selenium Import webdriver
 from selenium.webdriver.support.wait Import WebDriverWait
 from selenium.webdriver.support Import expected_conditions AS EC
 from selenium.webdriver.common.by Import By 

# create a Chrome objects 
driver = webdriver.Chrome ()
 # access Baidu 
driver .get ( ' http://baidu.com ' )
 # non-operating drop-down box select 
# find Baidu page setting button 
driver.find_element_by_xpath ( ' // div [@ ID = "U1"] // a [@ class = " PF "] ' ) .click ()
# Wait drop-down box appears 
LOC = (By.XPATH, ' // A [text () = "Advanced Search"] ' ) 
WebDriverWait (Driver, 10 ) .until (EC.visibility_of_element_located (LOC)) 
driver.find_element_by_xpath ( ' // a [text () = "advanced Search"] ' ) .click () 

# exit the browser 
driver.quit ()

 2, select drop-down box operations:

The next presentation select the drop-down box operation; to continue to use Baidu Settings - Advanced search page to demonstrate, continue to take the top continue to write the code

selenium has a select class for manipulating drop-down list select

For chestnut: operating under the red icon drop-down box

 

 

from the Selenium Import webdriver
 from selenium.webdriver.support.wait Import WebDriverWait
 from selenium.webdriver.support Import expected_conditions AS EC
 from selenium.webdriver.common.by Import By
 from selenium.webdriver.support.select Import the Select
 Import Time 


# Create a Chrome Object 
Driver = webdriver.Chrome ()
 # access Baidu 
driver.get ( ' http://baidu.com ' )
 # non-select drop-down box operation 
# find Baidu home page of the settings button
driver.find_element_by_xpath ( ' // div [@ ID = "U1"] // A [@ class = "PF"] ' ) .click ()
 # Wait down box appears 
LOC = (By.XPATH, ' // A [text () = "advanced Search"] ' ) 
WebDriverWait (Driver, 10 ) .until (EC.visibility_of_element_located (LOC)) 
driver.find_element_by_xpath ( ' // A [text () = "advanced Search"] ' ) .click () 

# set the wait, wait for the drop-down box select occur 
select_loc = (By.XPATH, ' // select [@ name = ". ft"] ' ) 
WebDriverWait (Driver, 10 ) .until (EC.visibility_of_element_located(select_loc))
ele = driver.find_element(*select_loc) 

S = the Select (ELE)
 # The following drop-down box subject method for positioning an element 
s.select_by_index (. 3 ) 
the time.sleep ( 2 )
 # manner attribute value of the drop-down box positioning element 
s.select_by_value ( ' PDF ' ) 
Time .sleep ( 2 )
 # text of the drop-down box positioned elements 
s.select_by_visible_text ( ' all formats ' ) 
the time.sleep ( 2 ) 

# exit the browser 
driver.quit ()

 

Guess you like

Origin www.cnblogs.com/xingyunqiu/p/11547235.html