Select多选框下拉列表
在做自动化的过程中,我们经常会遇到select标签类型的多选框下拉列表。针对select标签类型,selenium中有封装好的API可以使用,接下来,我们一起看看关于select多选框下拉列表的操作方法吧。
操作方法简介
1、选择下拉列表方法
selectByIndex(index); // 根据索引选择
selectByValue(value); // 根据value属性选择
selectByVisibleText(text); // 根据选项文本值选择
注意:
index是从0开始的
Value是option标签的一个属性值,并不是显示在下拉框中的值
VisibleText是在option标签中间的值,是显示在下拉框的值
2、取消方法
deselectByIndex(index); // 根据索引取消
deselectByValue(value); // 根据value属性取消
deselectByVisibleText(Text); // 根据选项文本值取消
deselectAll(); // 取消所有选中
3、其他方法
getAllSelectedOptions(); // 获取所有被选中的标签元素对象集合
getFirstSelectedOption(); // 获取第一个被选中的标签元素对象
select.getOptions(); // 获取所有选项的标签元素对象集合
getWrappedElement(); // 获取包裹具体选项的元素对象
isMultiple(); // 判断该元素是否支持多个对下个,根据mutiple属性来判断,返回布尔值
被测HTML对象
测试用的丐版html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
用户名: <input type="text" name=""/></br>
密 码: <input type="password" name=""/></br>
爱好: <input type="checkbox" name="">篮球<input type="checkbox" name="">电影</br>
性别: <input type="radio" name="gender">男<input type="radio" name="gender">女</br>
头像: <input type="file" name=""></br>
住址: <select id="select_province" name="select_name" class ="select_province_class">
<option value="0">安徽省</option>
<option value="1">江苏省</option>
<option value="2">广东省</option>
</select>
<select id="select_city" name="select_name" class ="select_city_class">
<option value="0">滁州市</option>
<option value="1">南京市</option>
<option value="2">广州市</option>
</select></br>
博客: <a href="https://www.cnblogs.com/91boge" target="_blank">点击进入</a></br>
备注信息: <textarea>社会人</textarea>
<form>
<table border="1">
<tr><td>姓名</td><td>年龄</td><td>分数</td></tr>
<tr><td>张三</td><td>28</td><td>100</td></tr>
<tr><td>李四</td><td>29</td><td>100</td></tr>
<tr><td>王二</td><td>29</td><td>100</td></tr>
<tr><td>麻子</td><td>29</td><td>100</td></tr>
</table>
<img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.diydoutu.com%2Fbqb%2F8031.jpg&refer=http%3A%2F%2Fimg.diydoutu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1652796255&t=863287e1bacb29390dfefc705fa184ce">
</form>
</body>
</html>
页面展示:
代码操作
1、选择下拉列表
@Test
public void testDemo1() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("E://Notes//Selenium//select.html");
driver.manage().window().maximize();
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("select_province"));
Select select = new Select(element);
// 选择索引值为2,即第三个选项
select.selectByIndex(2);
Thread.sleep(5000);
// 选择value属性值为1的选择
select.selectByValue("1");
Thread.sleep(5000);
// 选择文本值为安徽省的选项
select.selectByVisibleText("安徽省");
Thread.sleep(5000);
}
2、取消方法
取消方法针对的select标签是需要有multiple属性的,没有则抛异常。
在上面html中的select标签中增加multiple=”multiple”
<select id="select_province" name="select_name" class ="select_province_class" multiple="multiple">
<select id="select_city" name="select_name" class ="select_city_class" multiple="multiple">
@Test
public void testDemo2() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("E://Notes//Selenium//select_mutiple.html");
driver.manage().window().maximize();
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("select_city"));
Select select = new Select(element);
// 索引值为2的选择后取消
select.selectByIndex(2);
Thread.sleep(3000);
select.deselectByIndex(2);
Thread.sleep(3000);
// value属性值为1的选择后取消
select.selectByValue("1");
Thread.sleep(3000);
select.deselectByValue("1");
Thread.sleep(3000);
// 选择文本值为安徽省的选项,然后取消
select.selectByVisibleText("滁州市");
Thread.sleep(3000);
select.deselectByVisibleText("滁州市");
// 取消所有
select.deselectAll();
Thread.sleep(3000);
}
3、其他方法
@Test
public void testDemo() throws InterruptedException {
WebDriver driver = new ChromeDriver();
driver.get("E://Notes//Selenium//select.html");
driver.manage().window().maximize();
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("select_province"));
Select select = new Select(element);
boolean multiple = select.isMultiple();
System.out.println("multiple is " + multiple);
System.out.println("================================================");
WebElement wrappedElement = select.getWrappedElement();
System.out.println("wrappedElement is " + wrappedElement.getText());
System.out.println("================================================");
List<WebElement> options = select.getOptions();
System.out.println("The size of allSelectedOptions is " + options.size());
for(int i=0;i<options.size();i++) {
System.out.println("第"+ i +"次:" + options.get(i).getText());
}
System.out.println("================================================");
WebElement firstSelectedOption = select.getFirstSelectedOption();
System.out.println("firstSelectedOption is " + firstSelectedOption.getText());
System.out.println("================================================");
List<WebElement> allSelectedOptions = select.getAllSelectedOptions();
System.out.println("The size of allSelectedOptions is " + allSelectedOptions.size());
for(int i=0;i<allSelectedOptions.size();i++) {
System.out.println("第"+ i +"次:" + allSelectedOptions.get(i).getText());
}
driver.quit();
}
打印结果:
com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.test.day02.SelectTest,testDemo
Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664@{#947}) on port 37534
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1650212642.945][WARNING]: This version of ChromeDriver has not been tested with Chrome version 97.
四月 18, 2022 12:24:02 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: W3C
multiple is false
================================================
wrappedElement is 安徽省
江苏省
广东省
================================================
The size of allSelectedOptions is 3
第0次:安徽省
第1次:江苏省
第2次:广东省
================================================
firstSelectedOption is 安徽省
================================================
The size of allSelectedOptions is 1
第0次:安徽省
Process finished with exit code 0
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/245521.html