首页 > 编程语言 > 详细

python+selenium七:下拉框、选项框、select用法

时间:2018-11-22 01:01:59      阅读:193      评论:0      收藏:0      [点我收藏+]
 

技术分享图片

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Firefox()
url = "https://www.baidu.com"
driver.get(url)
time.sleep(3)


# 1、下拉框
mouse = driver.find_element("link text", "设置")
ActionChains(driver).move_to_element(mouse).perform()
time.sleep(0.5)
driver.find_element("link text", "搜索设置").click()
time.sleep(1)


# 方法一:直接定位
# 选择下拉框选项的第三项
driver.find_element_by_xpath(".//*[@id=‘nr‘]/option[3]").click()
# 若此时点击后,下拉选项未收回,可点击整个下拉框,收回下拉选项
driver.find_element_by_xpath(".//*[@id=‘nr‘]").click()

技术分享图片

 



# 方法二:二次定位
# 第一步:定位下拉框
parent = driver.find_element_by_id("nr")
# 第二步:在下拉框中,定位子元素,并操作
parent.find_element_by_xpath(‘.//option[@value="20"]‘).click()

技术分享图片

 





# select用法:
from selenium.webdriver.support.select import Select
# 先定位到下拉框
s = driver.find_element_by_id("nr")

# 第一种:根据索引定位(从0开始)
Select(s).select_by_index(0)
# 收回下拉选项
s.click()


# 第二种:根据value属性定位
# 如:value = 50
Select(s).select_by_value("50")
# 收回下拉选项
s.click()


# 第三种:根据选项内容定位
Select(s).select_by_visible_text("每页显示20条")
# 收回下拉选项
s.click()


技术分享图片

 


选项框:
选项框的另外一种形式(这种不叫select,跟普通定位一样)

技术分享图片

 

python+selenium七:下拉框、选项框、select用法

原文:https://www.cnblogs.com/dwdw/p/9998647.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!