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;
随机推荐
- numpy的介绍——总览
为什么有numpy这个库呢? 1. 准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[ ...
- Git 删除提交记录
.Checkout git checkout --orphan latest_branch 2. Add all the files git add -A 3. Commit the changes ...
- 127、Universal-Image-Loader解析(转载)(图片加载)
(一)——ImageLoaderConfiguration的详细配置http://www.cnblogs.com/tianzhijiexian/p/4034215.html (二)——DisplayI ...
- Xamarin Mono Android实现“再按一次退出程序”
开始研究Android平台软件编程,Xamarin Mono for Android上手快,跨平台共享代码,代价是bug多多,是一味可口的毒药啊! 环境VS2012 + Xamarin Mono An ...
- 一、K3 Wise 实施指导《K3 Wise实施手册》
1.总账期间启用后无法修改.固定资产期间启用后无法修改 ----修改总账 ' where fcategory='GL' and Fkey='startyear' --修改启用期间 ' where fc ...
- cube-ui修改按钮颜色
首先,当我们按照脚手架一步一步创建完项目以后 $ vue init cube-ui/cube-template projectname $ sudo npm install $ npm start 主 ...
- Linux中 SonarQube代码质量管理平台安装
SonarQube是管理代码质量一个开源平台,可以快速的定位代码中潜在的或者明显的错误. SonarQube安装 1.环境准备 (1)sonarQube 下载地址https://www.sonarqu ...
- 带分数|2013年蓝桥杯B组题解析第九题-fishers
带分数 100 可以表示为带分数的形式:100 = 3 + 69258 / 714 还可以表示为:100 = 82 + 3546 / 197 注意特征:带分数中,数字1~9分别出现且只出现一次(不包含 ...
- Linux下部署多个Tomcat(完整)
Linux下部署多个Tomcat 1.环境:1.1.Centos 5.01.2.apache-tomcat-6.0.18 2.需要解决一下几个问题2.1.不同的tomcat启动和关闭监听不同的端口2. ...
- Android超精准计步器开发-Dylan计步(申明:来源于网路)
Android超精准计步器开发-Dylan计步(申明:来源于网路) 拿来借鉴学习,向原创者... 地址:http://blog.csdn.net/linglongxin24/article/detai ...