driver.find_ Options = Elements _by_tag_name ( 'option') # get all option sub-elements
options [2] .click () # Get the third element
for i in range (len (options)): # value selection sub-item of value python
if options[i].get_attribute('value') == 'python':
options[i].click()
break
for i in range (len (options)): # selecting sub-items of text is python
if options[i].text == 'python':
options[i].click()
break
for i in range(len(options)):
if options[i].get_attribute('selected'):
print options [i] .get_attribute ( 'text') # Returns the text contents of the currently selected sub item
print options [i] .get_attribute ( 'value') # Returns the currently selected value of the child content
break
===========================================================================================
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
driver = webdriver.Firefox()
driver.get('http://www.baidu.com')
#--------------------------------------------------------------------------------------
link = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(link).perform() #鼠标悬停在上面
sleep(2)
driver.find_element_by_class_name("setpref").click() # 打开搜索设置
sleep(2)
#--------------------------------------------------------------------------------------------------
options = driver.find_Elements _by_xpath ( '// * [@ ID = "NR"] / Option')
Options [. 1] .click () # the second sub-item is selected
SLEEP (2)
Options [2] .click () # third sub item is selected
SLEEP (2)
Options [0] .click () # first sub-item is selected
SLEEP (2)
# ---------------------- -------------------------------------------------
Print (Options [0] .text)
Print (Options [. 1] .text)
Print (Options [2] .text)
# ----------------------- -------------------------------------------------- ---
Print ( '---------------------- 001')
for I in Range (len (Options)): # selecting sub-value is the python item
IF Options [I] .get_attribute ( 'value') == '20 is':
Options [I] .click ()
Print (Options [I].
BREAK
# ------------------------------------------------ ----------------------------
driver.quit ()
The results:
10 per
page shows 20
per page 50
---------------------- 001
per page 20