Java+selenium 如何定位下拉框select
场景:需要进行下拉选择定位元素。
一、select菜单
select也是比较常见的,selenium封装了以下方法, 创建select
WebElement selector = driver.findElement(By.id("Selector")); //Selector 表示定位的元素
Select select = new Select(selector);
选择select的option有以下三种方法
selectByIndex(int index) 通过index
selectByVisibleText(String text) 通过匹配到的可见字符
selectByValue(String value) 通过匹配到标签里的value
备注:
三、关键代码示例:
WebElement selector = waitFor(By.xpath("//select[@id='0']"));
Select sel = new Select(selector);
sel.selectByVisibleText(leaveType);
四、优化代码:
//优化前
WebElement selector = driver.findElement(By.xpath("//select[contains(@data-bind,'AuditType')]"));
Select sel = new Select(selector);
sel.selectByVisibleText(auditType); //优化后
selectByVisibleText(By.xpath("//select[contains(@data-bind,'AuditType')]"), auditType);
五: 调用方法
public void selectByVisibleText(By by, String text) {
Select sel = new Select(waitForShort(by));
sel.selectByVisibleText(text);
}
六 : 如何随机循环选择下拉框取值。
public void selectByRandomVisbleText(By by) {
Select sel = new Select(waitForShort(by));
//getOptions方法获取 WebElement得集合
List<WebElement> webEletments = sel.getOptions();
//新建List存储文本值
List<String> downs = new ArrayList<String>();
//循环webElement集合,将每个选项添加到List集合中。
for (WebElement webElement: webEletments) {
downs.add(webElement.getText());
System.out.println("下拉值" +webElement.getText());
}
//获取下拉值数据
int num = webEletments.size();
int random = Utils.getRandInt(0, num - 1);
//根据随机数选择
sel.selectByIndex(random);
}
Java+selenium 如何定位下拉框select的更多相关文章
- python+selenium七:下拉框、选项框、select用法
# from selenium import webdriverfrom selenium.webdriver.common.action_chains import ActionChainsimpo ...
- web自动化测试—selenium游览器下拉框操作
# coding=utf-8'''下拉框实战思路导包:from selenium.webdriver.support.select import Select #下拉框select from sele ...
- selenium-Python之定位下拉框选择
1.通过select 进行定位下拉框 下拉框如图所示 通过代码定位 #通过index进行选择Select(driver.find_element_by_id("cardType") ...
- jQuery对下拉框Select操作总结
jQuery对下拉框Select操作总结 转自网络,留做备用 jQuery获取Select元素,并选择的Text和Value: 1. $("#select_id").change( ...
- struts2 jsp表单提交后保留表单中输入框中的值 下拉框select与input
原文地址:struts2 jsp表单提交后保留表单中输入框中的值 下拉框select与input jsp页面 1 function dosearch() {2 if ($(&q ...
- JQuery操作下拉框 select
要实现这种效果: html代码 1<script src="js/jquery-1.7.2.min.js"></script> 2 <table> ...
- 基于定位下拉框或者需要点击link才显示的下拉框,二次定位与多次定位实现的实际效果区别
还是基于上次那个练习的后续出现的思考,http://www.cnblogs.com/8013-cmf/p/6555790.html 界面: 源码: 写法如下: 继续解释这两种的区别: 1.其实基于定 ...
- 百度“搜索设置”之基于定位下拉框或者需要点击link才显示的下拉框,二次定位与多次定位实现的实际效果区别
还是基于上次那个练习的后续出现的思考,http://www.cnblogs.com/8013-cmf/p/6555790.html 界面: 源码: 写法如下: 继续解释这两种的区别: 1.其实基于定 ...
- 下拉框select中option居中样式
下拉框select中option居中样式 text-align:center;text-align-last:center;
随机推荐
- centos 6.4 使用epel 源
EPEL是RHEL 的 Fedora 软件仓库,把它添上,你就可以获得 RHEL AS 的高质量.高性能.高可靠性,又需要方便易用(关键是免费)的软件包更新功能.EPEL(Extra Packages ...
- TI am335x am437x PRU
http://bbs.eeworld.com.cn/thread-355798-1-1.html
- IOS Swift 训练
// Playground - noun: a place where people can play import Cocoa var str = "Hello, playground&q ...
- Foxpro数据库连接错误解决方法--【VFP DBF文件不是一个有效的路径。 确定路径名称拼写是否正确,以及是否连接到文件存放的服务器】
直接访问vfp dbf文件时报错: 错误描述: 'd:\vfpData\test.dbf'不是一个有效的路径. 确定路径名称拼写是否正确,以及是否连接到文件存放的服务器. 解决办法:Data Sour ...
- Erlang的gen_server的terminate()/2未执行
官方资料参考: Module:terminate(Reason, State) Types: Reason = normal | shutdown | {shutdown,term()} | term ...
- PDF Transformer+与Transformer3.0功能对比
ABBYY PDF Transformer+是一个新的.全面的巧妙解决PDF文档的工具,它将泰比的光学字符识别(OCR)技术和Adobe®PDF技术完美结合,以确保实现便捷地处理任何类型的PDF文件, ...
- Git错误:error: The following untracked working tree files would be overwritten by merge:
[andy@localhost weixin_robot]$ git pull Updating d652d1c..fa05549 error: The following untracked wor ...
- [UFLDL] Dimensionality Reduction
博客内容取材于:http://www.cnblogs.com/tornadomeet/archive/2012/06/24/2560261.html Deep learning:三十五(用NN实现数据 ...
- [Python] 08 - Classes --> Objects
故事背景 一.阶级关系 1. Programs are composed of modules.2. Modules contain statements.3. Statements contain ...
- java基础---->序列化框架arvo的使用
这里面我们介绍一下序列化框架arvo的使用,在kafka中使用的就是这个. arvo的使用 一.需要通过插件生成Model类方式 一.生成我们的数据模型User.java 我们在resources里面 ...