添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
备案 控制台
学习
实践
活动
专区
工具
TVP
写文章
专栏首页 Urlteam Python Selenium下拉列表元素定位
2 0

海报分享

Python Selenium下拉列表元素定位

对于select>option结构的下拉列表定位总结以下两种方法:

1.定位父元素select,然后通过tag name找到所有option,得到option元素的数组,然后通过数组索引定位,最后click.

driver.find_element_by_id("test").find_elements_by_tag_name("option")[0].click();

2.用到了Select类, 实例select对象有很多方法:

  deselect_all(),全不选。
  deselect_by_index(index),不选第 index 项。或者是 index+1项,忘了index从0 还是从1 开始了。
  deselect_by_value( value),不选元素value属性为value的项,听着有点拗口,其实value值就是option标签中value的值。
  deselect_by_visible_text( text),不选标签innerHTML为text的option
  select_by_index( index),同上,选择第 index 项。这个用于 option的text和value不固定的情况
  select_by_value( value), 同上,选择。
  select_by_visible_text( text),同上,选择。

3.使用示例如下:

from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id("test"))
select.select_by_visible_text("Edam")

4.不仅是click可以,同样的text等方法也是通用:

title1 = driver.find_element_by_xpath('//*[@id="cateid"]').find_elements_by_tag_name("option")[1].text  #text 获取该下拉行的文本内容
driver.find_element_by_xpath('//*[@id="cateid"]').find_elements_by_tag_name("option")[1].click() #click 模拟点击
time.sleep(2)

原创文章,转载请注明: 转载自 URl-team

本文链接地址: Python Selenium下拉列表元素定位

本文参与 腾讯云自媒体分享计划 ,欢迎热爱写作的你一起参与!
本文分享自作者个人站点/博客: 复制
如有侵权,请联系 cloudcommunity@tencent.com 删除。