selenium Select下拉框

先来认识一下下拉框,以百度的“高级设置”为例

介绍两种方法来处理下拉框:使用click事件,使用Select方法

  • 使用click事件

上述下拉框的源代码如下:

虽然我们可以在html源文件中看到select的各个选项,但是,如果我们没有定位到该下拉框的话,是定位不到里面的子选项的,

所以使用click事件,需要一步一步的点击

from selenium import webdriver
driver=webdriver.Firefox()
driver.get("https://www.baidu.com/gaoji/advanced.html")

#定位搜索网页格式下拉框
driver.find_element_by_name("ft").click()
#通过xpath定位子选项
option1=driver.find_element_by_xpath("//*[@name=‘ft‘]/option[1]")
option2=driver.find_element_by_xpath("//*[@name=‘ft‘]/option[2]")
option3=driver.find_element_by_xpath("//*[@name=‘ft‘]/option[3]")
option4=driver.find_element_by_xpath("//*[@name=‘ft‘]/option[4]")
#打印选项
print(option1.text)
print(option2.text)
print(option3.text)
print(option4.text)
driver.quit()
  • 使用Select方法
from selenium import webdriver
#导入select模块
from selenium.webdriver.support.select import Select

driver=webdriver.Firefox()
driver.get("https://www.baidu.com/gaoji/advanced.html")
#d定位下拉框
s=driver.find_element_by_name("ft")
# #通过索引号,值从0开始,每一条option为一个索引
option1=Select(s).select_by_index(0)
option2=Select(s).select_by_index(2)
# 通过value值,每个option都会有的属性
option3=Select(s).select_by_value("pdf")
option4=Select(s).select_by_value("doc")
#通过文本,直接通过选项的文本来定位
option5=Select(s).select_by_visible_text("微软 Powerpoint (.ppt)")
option6=Select(s).select_by_visible_text("RTF 文件 (.rtf)")

Select还提供了其他方法

Select(s).all_selected_options  #返回所有选中的选项
Select(s).options               #f返回所有选项
Select(s).first_selected_option #返回该下拉框第一个选项
Select(s).deselect_by_index()   #取消所有选项

还有很多,不再一一列举,有兴趣可以自行研究

时间: 2024-07-29 03:28:11

selenium Select下拉框的相关文章

11 Python+selenium对下拉框(select)进行处理

[环境信息] Python3.4+IE+windows2008 [Select下拉框处理] 1.对于如图1的下拉框,可以用selenium自带的Select类进行选择. 2.定位示例: from selenium.webdriver.support.select import Select # 责任部门 Select(self.driver.find_element_by_id('linkDutyDept')).select_by_visible_text('市场经营部门') 3.说明 Sele

selenium webdriver学习(八)------------如何操作select下拉框(转)

selenium webdriver学习(八)------------如何操作select下拉框 博客分类: Selenium-webdriver 下面我们来看一下selenium webdriver是如何来处理select下拉框的,以http://passport.51.com/reg2.5p这个页面为例.这个页面中有4个下拉框,下面演示4种选中下拉框选项的方法.select处理比较简单,直接看代码吧:) Java代码   import org.openqa.selenium.By; impo

Python3.x:selenium遍历select下拉框获取value值

Python3.x:selenium遍历select下拉框获取value值 Select提供了三种选择方法: # 通过选项的顺序,第一个为 0 select_by_index(index) # 通过value属性 select_by_value(value) # 通过选项可见文本 select_by_visible_text(text) Select提供了四种方法取消选择: deselect_by_index(index) deselect_by_value(value) deselect_by

Selenium系列(十) - 针对Select下拉框的操作和源码解读

如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识,需要自己去补充哦,博主暂时没有总结(虽然我也会,所以我学selenium就不用复习前端了哈哈哈...) 首先,将下面html代码保存到一个文件中 后续的代码小案例都是访问此html的<!DOCTYPE html> <html lang="en"> <head&

JavaScript获取Select下拉框Option的Value和Text值的方法

Js获取select下拉列表框各个Option的Value值相对比较容易,不过获取Text值却有点麻烦,对于一个初学JavaScript的 新手来说,可能一时还无从下手,那么就请看下本文的方法,以一个form表单中的Select下拉框菜单为例,来说明如何用JavaScript获取其 Value值和Text值: 示例表单,里面是一个select下拉列表框的各个列表项及值: <form name="form1"> <select name="testvalue&

Javascript获取select下拉框选中的的值

现在有一id=test的下拉框,怎么拿到选中的那个值呢? 分别使用javascript原生的方法和jquery方法 <select id="test"  name="">     <option   value="1">text1</option>     <option   value="2">text2</option>    </select> co

jQuery制作简洁的多级联动Select下拉框

今天我们要来分享一款很实用的jQuery插件,它是一个基于jQuery多级联动的省市地区Select下拉框,并且值得一提的是,这款联动下拉框是经过自定义美化过的,外观比浏览器自带的要漂亮许多.另外,这个Select下拉框也可以绑定下拉事件,并获取当前选中项的值. html代码: <div class="wrap">        <div class="nice-select" name="nice-select">   

模拟select下拉框之多选(数据源采用模拟Ajax数据--原创)

最近需要一个下拉多选,本来想偷懒的,所以在网上百度了一番,最终还是发现没有一个符合自己要求的,所以我自己写了一个插件.下面是GIF动态效果图展示 相信大家已经看到效果了,接下来就是我的代码展示 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>模拟select下拉框之多选</title> <style

去除select下拉框默认样式

去除select下拉框默认样式 select { /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ border: solid 1px #000; /*很关键:将默认的select选择框样式清除*/ appearance:none; -moz-appearance:none; -webkit-appearance:none; /*将背景改为红色*/ background:red; /*加padding防止文字覆盖*/ padding-right: 14px; } /*清除