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;
随机推荐
- 【iCore1S 双核心板】DEMO V1.0 测试程序发布
iCore1S Demo V1.0程序说明 一.概要 本资料包含5个文件夹: 1.“ARM”里是iCore1S上ARM的程序包,开发环境为KEIL5.17: 2.“FPGA”里是iCore1S上FPG ...
- Linux下MySQL5.7.18二进制包安装(无默认配置文件my_default.cnf)
最新在学习MySQL,纯新手,对Linux了解的也不多,因为是下载的最新版的MySQL(MySQL5.7.18)二进制包,CentOS7.2下测试安装,方便以后折腾.大概步骤如下,安装删除反复折腾了几 ...
- react给一个div行内加背景图片并实现cover覆盖模式居中显示
具体background简写可以参考这篇文章. 这里注意,如果简写里要写background-size,则这里必须写 / ,否则整个背景图片样式没有解析出来. 它和font以及border-radi ...
- HTTPS与证书
HTTPS(Secure Hypertext Transfer Protocol)安全超文本传输协议它是一个安全通信通道,它基于HTTP开发,用于在客户计算机和服务器之间交换信息.它使用安全套接字层( ...
- error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2
使用VS2013版本引用外部的lib进行编译时候提示: 错误 25 error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2”(jrtpl ...
- 【netcore入门】在Windows IIS上部署.NET Core 2.1项目
部署之前先检查下面2个先决条件是否满足 1.安装了 IIS 模块 win7 在 控制面板→程序和功能→打开或关闭Windows功能→勾选Internet 信息服务(Internet Informati ...
- LeetCode - 872. Leaf-Similar Trees
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form ...
- 转载->CPU的内部架构和工作原理
CPU的内部架构和工作原理 本片博客转自:http://www.cnblogs.com/onepixel/p/8724526.html 感谢博主分享! 内部架构 CPU 的根本任务就是执行指令,对计 ...
- nuxt项目中vue报错The client-side rendered virtual ...
报错: 翻译过来是: [Vue警告]:客户端呈现的虚拟DOM树与服务器呈现的内容不匹配.这可能是由不正确的HTML标记引起的,例如在其中嵌套块级元素或丢失.Bailing水化和执行完整的客户端渲染. ...
- 使用 Markdown 写技术博客,踩过的 6个坑
目录 Markdown 特性 Markdown 简介 常用语法 为什么流行 设计哲学 工具支持 版本演进 标准化之路 踩过了坑 平台帮助文档 语法差异 显示效果 我的最佳实践 摘要:本文记录我在使用 ...