当前位置:网站首页>web自动化:6.selenium下拉选择框的操作-Select
web自动化:6.selenium下拉选择框的操作-Select
2022-04-22 06:32:00 【花测试】
在做web自动化测试时,下拉框肯定是难以避免的,简单讲下其实现方式
下拉选择框,前端实现的2种方式:
1、原生方式
<select> </select>
2、通过js控制选项的显示或隐藏来达到与select标签同样的效果
<div> </div>
下拉框选择的实现方法
1、常规操作,适用于以上两种方式
先定位并点击选择框元素;待下拉框选项展示出来后,再定位并点击选项元素。
2、Select类,仅适用于select标签实现的下拉框
原理其实就是常规操作的步骤,只是selenium提供了Select类,将代码封装起来使用比较方便。
练习
我自己用python语言写一个简单的html文件,方便练习,代码如下:
<html lang="en">
<head>
<meta charset="UTF-8">
<title>select选择框的操作</title>
</head>
<body>
<h6>花测试 select选择框的操作</h6>
<select id="test">
<option value="hsl">何时了</option>
<option value="qy">秋月</option>
<option value="ch">春花</option>
</select>
</body>
</html>
1、常规操作
from selenium import webdriver
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
url = '根据实际填写:在本地打开上面的html文件,然后复制浏览器上的url粘贴过来'
driver.get(url)
# 常规通用法
select_cl = driver.find_element('id', 'test')
select_cl.click()
select_cl = driver.find_element('xpath', '//option[text()="秋月"]')
select_cl.click()
2、使用Select类
from selenium import webdriver
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.maximize_window()
url = '根据实际填写:在本地打开上面的html文件,然后复制浏览器上的url粘贴过来'
driver.get(url)
select_cl = driver.find_element('id', 'test') # 定位选择框元素
s = Select(select_cl) # 初始化一个 select 对象
# 方法1:通过option选项中的 value 属性选中选项
s.select_by_value('qy')
# 方法2:通过索引选中选项
s.select_by_index(1) # 从0开始
# 方法3:通过文本选中选项
s.select_by_visible_text('秋月')
版权声明
本文为[花测试]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_48415452/article/details/120163959
边栏推荐
- 《数据安全产品指南》正式发布,世平信息多项产品收录其中
- .Net5 Log4Net启动一段时间后记录日志到数据库中失败问题
- web自动化:8.2 selenium中如何发送js指令
- Raspberry Pie: 4.2-inch ink screen
- Configuration list of raspberry pie (entry reference)
- 数据库原理课程设计——物资发放管理系统
- 华为手机adb devices连接设备为空
- Practical application of callable combined with countdownlatch
- js动态合并单元格
- Raspberry Pie: adafruit's I2C ssd1306 OLED driver
猜你喜欢

Shrio 学习笔记(二)

Application of Wireshark in traffic analysis

Practical application of callable combined with countdownlatch
![[Shiping information] solutions for confidentiality inspection and compliance control of recorded content](/img/45/bb2cc00e6d61c7c18268336d00101f.png)
[Shiping information] solutions for confidentiality inspection and compliance control of recorded content

八阿哥纪事【三】

On June 1st, write a poem

Call another function within a shell function (without return value and with return value)

朴素的CSRF漏洞

Ultra vires and business logic vulnerabilities

ik分词器-简单使用
随机推荐
《数据安全产品指南》正式发布,世平信息多项产品收录其中
MySql查询指定一行排序到第一行
Raspberry pie 4B: USB boot using raspi config
世平信息数据安全合规检测实践论文入选中文核心期刊
Import 3D model from web page - obj / MTL file
小菜鸡的学习笔记——sql注入之sqli-lab边学边练
web自动化:8.2 selenium中如何发送js指令
OBD external test equipment initialization judgment protocol type (SAE J1939 / iso15765 / iso27145)
.Net5中使用Swagger
登录界面——渗你千千万万遍
世平信息董事长王世晞荣获2020年度新锐人物奖
Plain CSRF vulnerability
世平信息亮相中国中医药信息大会,助力医药行业数据安全体系建设
[interview question] move all zeros of an unordered shaping array to the end, and keep the relative order of other non-zero elements unchanged
.net 5 Web自定义中间件实现返回默认图片
MNIST handwritten numeral recognition based on convolutional neural network lenet-5 model
LDAP用户登录认证校验及查询
ik分词器-简单使用
On June 1st, write a poem
Wangshixi, chairman of Shiping information, won the 2020 new person Award