Selenium下拉菜单(Select)的操作-----Selenium快速入门(五)
对于一般元素的操作,我们只要掌握本系列的第二,三章即可大致足够。对于下拉菜单(Select)的操作,Selenium有专门的类Select进行处理。文档地址为:http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/Select.html
该类只有一个构造函数,Select(WebElement element),如果我们定位的元素并非Select,则会引发异常:UnexpectedTagNameException - when element is not a SELECT
要懂得Select类的方法的使用,首先要先知道Html中,Select标签的用法。在此不啰嗦,简单提一下就是,Select有两种模式,单选和多选,单选模式就是我们经常见到的下拉框,多选模式,在Select标签中增加 multiple,例如<select name="phones" multiple>,显示的样式如下图,多选的时候,需要按住ctrl+左键点击

下面说说Select类的常用方法。
| 方法 | 说明 |
| void deselectAll() | 取消所有选择项,仅对下拉框的多选模式有效,若下拉不支持多选模式,则会抛出异常 UnsupportedOperationException(不支持的操作) |
| void deselectByIndex(int index) | 取消指定index的选择,index从零开始,仅对多选模式有效,否则抛出异常 UnsupportedOperationException(不支持的操作) |
| void deselectByValue(String value) | 取消Select标签中,value为指定值的选择,仅对多选模式有效,否则抛出异常 UnsupportedOperationException(不支持的操作) |
| void deselectByVisibleText(String Text) | 取消项的文字为指定值的项,例如指定值为Bar,项的html为 <option value="foo">Bar</option>,仅对多选模式有效,单选模式无效,但不会抛出异常 |
List<WebElement>getAllSelectedOptions() |
获得所有选中项,单选多选模式均有效,但没有一个被选中时,返回空列表,不会抛出异常 |
WebElement getFirstSelectedOption() |
获得第一个被选中的项,单选多选模式均有效,当多选模式下,没有一个被选中时,会抛出NoSuchElementException异常 |
List<WebElement>getOptions() |
获得下拉框的所有项,单选多选模式均有效,当下拉框没有任何项时,返回空列表,不会抛出异常 |
boolean isMultiple() |
判断下拉框是否多选模式 |
| void selectByIndex(int index) | 选中指定index的项,单选多选均有效,当index超出范围时,抛出NoSuchElementException异常 |
| void selectByValue(String value) | 选中所有Select标签中,value为指定值的所有项,单选多选均有效,当没有适合的项时,抛出NoSuchElementException异常 |
| void selectByVisibleText(String text) | 选中所有项的文字为指定值的项,与deselectByValue相反,但单选多选模式均有效,当没有适合的项时,抛出NoSuchElementException异常 |
看起来似乎很难记,实际很容易,select开头的,单选多选均有效;deselect开头的,仅对多选模式有效;返回选中项的,单选多选均有效,返回List的不会抛出异常。
下面用简单例子,展示如何操作下拉菜单。我们在项目文件夹下,添加html文件夹,并增加一个dropdown.html,文件结构如下

dropdown.html的html代码为
<!DOCTYPE html>
<html>
<head>
<meta>
<title>下拉列表</title>
</head>
<body>
<form action="">
<select name="phones" multiple>
<option value="华为">华为</option>
<option value="三星">三星</option>
<option value="中兴">中兴</option>
<option value="小米">小米</option>
</select>
</form> </body>
</html>
我们的目标是选中“中兴”,代码为
//得到WebDriver
WebDriver driver=DriverHelper.CreateChromeDriver();
//转到我们刚才编写的html
driver.get("D:/WorkSpace/SeleniumTest/html/dropdown.html");
//找到下拉框元素
WebElement element=driver.findElement(By.name("phones"));
//转化为Select
Select select=new Select(element);
//使用value也可以
select.selectByValue("中兴");
//使用可见文字也可以
//select.selectByVisibleText("中兴");
//使用index也可以
//select.selectByIndex(2);
最终执行的效果如下:

Selenium下拉菜单(Select)的操作-----Selenium快速入门(五)的更多相关文章
- 下拉菜单select高度(兼容IE6/IE7/IE8/火狐等主流浏览器)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Python+selenium下拉菜单选项
案例:在我要自学网登录页面选择要保留的时间 具体页面如图所示: 使用前端工具查看部分页面代码: <select class="loinp" name="Cookie ...
- selenium下拉菜单
from selenium.webdriver.support.select import Selectdef select_value(self, css, value): ''' 选中 ...
- jsp中将后台传递过来的json格式的list数据绑定到下拉菜单select
<span style="white-space:pre"> </span> <select><c:forEach var="f ...
- 2016/3/24 ①数据库与php连接 三种输出fetch_row()、fetch_all()、fetch_assoc() ②增删改时判断(布尔型) ③表与表之间的联动 ④下拉菜单 ⑤登陆 三个页面
①数据库与php连接 图表 header("content-type:text/html;charset=utf-8"); //第一种方式: //1,生成连接,连接到数据库上的 ...
- 如何在webapp中做出原生的ios下拉菜单效果
github:https://github.com/zhoushengmufc/iosselect webapp模仿ios下拉菜单 html下拉菜单select在安卓和IOS下表现不一样,iossel ...
- DOM(十)使用DOM设置单选按钮、复选框、下拉菜单
1.设置单选按钮 单选按钮在表单中即<input type="radio" />它是一组供用户选择的对象,但每次只能选一个.每一个都有checked属性,当一项选择为t ...
- vue实现隔行换色,下拉菜单控制隔行换色的颜色
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- selenium基础(下拉菜单操作)
selenium基础(下拉菜单操作) 非select/option元素: 1.触发下拉列表出现 2.等待下拉列表中的元素出现,然后进行选择元素即可. select/option元素: 下拉框操作-Se ...
随机推荐
- 【洛谷】P1247 取火柴游戏(Nim)
题目 传送门:QWQ 分析 蒟蒻根本不会博弈论..... 只知道异或和判断Nim游戏.. 不是很懂输出的选择,所以发一篇博客以待复习 代码 #include <bits/stdc++.h> ...
- 【POJ】1935 Journey(树形dp)
题目 传送门:QWQ 分析 凉凉. 答案是所有要经过的点到根所经过的边权和减去最大的边权. 代码 vector好慢啊 #include <cstdio> #include <vect ...
- nginx收到空包问题
tcpdump有收包,但是nginx的access.log显示post数据为空 可以通过tcpdump监控端口 http://www.cnblogs.com/linn/p/4792468.html 修 ...
- A configuration error occurred during startup.Please verify the preference filed with the prompt:Connect to VM
1. 检查JDK,及Tomcat是否正确可用.2. Tomcat,myeclipse使用的是不是同一个jdk.3. 检查系统的防火墙是不是阻止了MyEclipse主程序访问网络.
- Maven - Deploy war in Tomcat 7 & 8
This article will explain on how to deploy a war fine in to Tomcat 7 through maven build. Note : I h ...
- How to run eclipse in clean mode? and what happens if we do so?
What it does: if set to "true", any cached data used by the OSGi framework and eclipse run ...
- c# 之 new 关键字
1.实例化变量 DataTable dt = new DataTable(); 2.调用构造函数 class CoOrds { public int x, y; // 实例构造函数(默认构造函数) ...
- (转存)面向切面编程(AOP)的理解
面向切面编程(AOP)的理解 标签: aop编程 2010-06-14 20:17 45894人阅读 评论(11) 收藏 举报 分类: Spring(9) 在传统的编写业务逻辑处理代码时,我们通常 ...
- python实例、类方法、静态方法
[python实例.类方法.静态方法] 参考:http://blog.163.com/yang_jianli/blog/static/161990006201122411586729/
- 40. Combination Sum II (Back-Track)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...