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;
随机推荐
- vue事件处理器
1.监听事件 可以用 v-on 指令监听 DOM 事件来触发一些 JavaScript 代码. 示例: <div id="example-1"> <button ...
- Java知多少(59)创建多线程
到目前为止,我们仅用到两个线程:主线程和一个子线程.然而,你的程序可以创建所需的更多线程.例如,下面的程序创建了三个子线程: // Create multiple threads. class New ...
- Mysql系列三:Centos6下安装Mysql和Mysql主从复制的搭建
一.Centos6下安装Mysql 检测下系统有没有自带的mysql:yum list installed | grep mysql, 如果已经有的话执行命令yum -y remove mysql-l ...
- input 手机号
关于input手机号的验证 一.手机号的判断方法: function isPhoneTel(n){ var reg = /^1[3|4|5|8]\d{9}$/; if(!!(reg.test(n))) ...
- react报错this.setState is not a function
当报错这个的时候就要看函数是否在行内绑定this,或者在constructor中绑定this. 我这里犯的错误的是虽然我在constructor中绑定了this,但是语法写的不正确. 错误示范: co ...
- [PHP] 03 - Form & Input
PHP 完整表单实例 一.表单示范 二.对应代码 <!DOCTYPE HTML> <html> <head> <meta charset="utf- ...
- 网络编程 -- RPC实现原理 -- NIO多线程 -- 迭代版本V2
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——增加WriteQueue队列,存放selectionKey.addWriteEventToQueue()添加selectionKey并唤醒阻 ...
- Kubernetes部署SpringCloud(三) 使用 Ingress-nginx 暴露服务
之前部署的zuul以及basic-info-api 都仅仅在于flannel 网络内可以访问. 现在来使用Ingress-nginx 对外暴露服务 以下用到的一些docker镜像,是存在我私有仓库的, ...
- 如何用VBA实现格式刷的功能?
1. http://club.excelhome.net/thread-373686-1-1.html 复制1格式Selection.CopyFormat粘贴格式Selection.PasteForm ...
- linux 下开源代理路由工具
服务器搭建,参考 https://gfw.press/blog/?p=21 运行环境 openjdk1.8,linux 1.首先,获取工具地址 git clone https://github.com ...