Selenium+python --定位下拉列表框并选取内容

follow yoyo

定位下拉列表并选取内容

# 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()
driver.get("https://baidu.com")
driver.implicitly_wait(20)

# 鼠标移动到设置按钮
mouse = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(mouse).perform()
driver.find_element_by_link_text("搜索设置").click()

# 方法一:定位到下拉框,再点击选项
# s = driver.find_element_by_id("nr")
# s.find_element_by_xpath("//option[@value='50']").click()
driver.find_element_by_id("nr").find_elements_by_xpath("//option[@value='50']").clear()

# 方法二:使用xpath/css定位
driver.find_elements_by_xpath("//*[@id='nr']/option[2]").click()

# 方法三:使用Select模块by index
s = driver.find_element_by_id("nr")
Select(s).select_by_index(2)

# select by value
Select(s).select_by_value("20")

# select by text
Select(s).select_by_visible_text("每页显示20条")


猜你喜欢

转载自www.cnblogs.com/shanliguniang/p/10654030.html