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;
随机推荐
- npm国内镜像
国内使用默认的源安装较慢,镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set registry h ...
- Sql Server 数据类型与 C# 数据类型对照
Sql Server 数据类型与 C# 数据类型对照 已验证类型(Sql Server 2012 & Visual Studio 2013) Sql Server C# 简写 bigint S ...
- 小程序url传参如何写变量
<navigator url="../../pages/newsDetail/newsDetail?id={{news.id}}"> <view class=&q ...
- [Ubuntu] LightDM 轻量级桌面显示管理器
LightDM(Light Display Manager)是一个全新的轻量级 Linux 桌面显示管理器,而传统的 Ubuntu 是使用 GNOME 桌面标准的 GDM. LightDM 是一个跨桌 ...
- JS document.execCommand实现复制功能(带你出坑)
最近项目中需要实现功能:点击button,复制input框的值: 我使用的是 document.execCommand('copy')的方法: 但是很郁闷的是,始终实现不了功能:代码如下 HTML代码 ...
- 十一、K3 WISE 开发插件《VB插件开发如何代码调试 - 步骤讲解》
=================================== 目录: 1.配置代码调试启动程序kdmain.exe 2.设置断点 3.触发调试 4.变量跟踪 ================ ...
- python金融与量化分析------Matplotlib(绘图和可视化)
-----------------------------------------------------------Matplotlib:绘图和可视化------------------------ ...
- 编译openssl失败(SLES11.3), undefined reference to `OPENSSL_cpuid_setup'
https://stackoverflow.com/questions/11381514/undefined-reference-when-compiling-openssl I ran into t ...
- 23. 合并K个排序链表
一种方法是分治 类似快排的例子. 第二种使用堆,比较好理解. 堆中保存一个元素是一个链表的头部. /** * Definition for singly-linked list. * public ...
- maven工程之pom模板(hadoop、hive、hbase)
以下配置文件涵盖了hadoop.hive.hbase开发支持库的配置. 仅需针对maven工程pom.xml文件做相应更改就可以自动生成hadoop开发支持库. <properties> ...