seleniumu 3.0复选框操作(定位一组元素)

2023-06-12,,

一般验证复选框是否可以选择点击常用到定位一组元素去循环遍历执行点击事件。但是有时候在不同的浏览器下可能会存在差异化的最终结果。

目前谷歌浏览器常常存在多次点击同一复选框,导致最终最后两项复选框均未被勾选。但是在火狐浏览器所有的复选框均被勾选上,火狐可以和IE一起,直接在添加IE TAB插件即可。

HTML代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Checkbox</title>
<link
href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"
rel="stylesheet" />
<script
src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></script>
</head>
<body>
<h3>checkbox</h3>
<div class="well">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="c1">checkbox1</label>
<div class="controls">
<input type="checkbox" id="c1" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="c2">checkbox2</label>
<div class="controls">
<input type="checkbox" id="c2" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="c3">checkbox3</label>
<div class="controls">
<input type="checkbox" id="c3" />
</div>
</div>
</form>
</div>
</body>
</html>

谷歌浏览器代码如下:

public static void main(String[] args) throws IOException, InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:/chromedriver_win32/chromedriver.exe");
ChromeOptions Options = new ChromeOptions();
Options.addArguments("user-data-dir=C:\\Users\\happy\\AppData\\Local\\Google\\Chrome\\User Data");
WebDriver driver = new ChromeDriver(Options);
try {
File file = new File("C:/Users/happy/Desktop/NewFile.html");
// Java 流(Stream) 中的File类
String filepath = file.getAbsolutePath();
// 获取文件的绝对路径
driver.get(filepath);
// 在浏览器中打开相关文件NewFile.html
List<WebElement> inputs = driver.findElements(By.tagName("input"));
// 把所有的input标签放入List集合。然后用foreach 遍历元素。
for (WebElement checkbox : inputs) {
String type1 = new String(checkbox.getAttribute("type"));
if (type1.equals("checkbox")) {
System.out.println(checkbox.getAttribute("id"));
// 打印出执行点击操作的元素的id
checkbox.click();
}
} } finally {
Thread.sleep(10000);
driver.close();
driver.quit();
}
}

火狐浏览器

public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "D:/firefox-47.0.1.win64.sdk/firefox-sdk/bin/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
try {
// File file = new File("C:/Users/happy/Desktop/NewFile.html");
// // Java 流(Stream) 中的File类
// String filepath = file.getAbsolutePath();
// 获取文件的绝对路径
driver.get("file:///c:/Users/happy/Desktop/NewFile.html");
// 在浏览器中打开相关文件NewFile.html
List<WebElement> inputs = driver.findElements(By.tagName("input"));
// 把所有的input标签放入List集合。然后用foreach 遍历元素。
for (WebElement checkbox : inputs) {
String type1 = new String(checkbox.getAttribute("type"));
if (type1.equals("checkbox")) {
System.out.println(checkbox.getAttribute("id"));
// 打印出执行点击操作的元素的id
checkbox.click();
}
}
} finally{ driver.close();
driver.quit();
} }

driver.navigate().refresh();
这个方法用于刷新页面。
inputs.size()
size()方法可以计算获得元素的个数。这里获得的结果为3。3 减1 为2。

inputs.get().click();//集合的get()方法,可以直接对指定元素进行操作。获取集合中的指定元素。
get()指定得到元素组中的第几个元素,并对其时行click()操作。

seleniumu 3.0复选框操作(定位一组元素)的相关教程结束。

《seleniumu 3.0复选框操作(定位一组元素).doc》

下载本文的Word格式文档,以方便收藏与打印。