selenium测试(Java)-- 一组元素操作(十一)
2024-08-30 08:27:59
利用下面的例子来编写测试脚本
页面代码:
<!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-lable" for="c1">checkbox1</label>
<div class="controls">
<input type="checkbox" id="c1" />
</div>
</div>
<div class="control-group">
<label class="control-lable" for="c2">checkbox2</label>
<div class="controls">
<input type="checkbox" id="c2" />
</div>
</div>
<div class="control-group">
<label class="control-lable" for="c3">checkbox3</label>
<div class="controls">
<input type="checkbox" id="c3" />
</div>
</div>
</form>
</div>
</body>
</html>
脚本:
package com.test.findelement; import java.util.Iterator;
import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver; public class CheckBox { public static void main(String[] args) {
// TODO Auto-generated method stub WebDriver driver = new FirefoxDriver();
driver.get("file:///D:/10-selenium/workspace/SeleniumTest/src/com/test/findelement/checkbox.html");
driver.manage().window().maximize(); List<WebElement> WebElements = driver.findElements(By.cssSelector("[type=checkbox]")); Iterator iterator = WebElements.iterator(); //全部勾选
while(iterator.hasNext()){
WebElement wb = (WebElement)iterator.next();
wb.click();
}
//去掉第一个
waitTime(3000);
driver.findElements(By.cssSelector("[type=checkbox]")).get(0).click(); //去掉最后一个
waitTime(3000);
driver.findElements(By.cssSelector("[type=checkbox]")).get(WebElements.size()-1).click(); waitTime(5000);
driver.quit();
} static public void waitTime(int time) { try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
最新文章
- NSURLSession详解
- Django分析之三级下拉菜单选择省/市/县
- iOS 面试题(二):什么时候在 block 中不需要使用 weakSelf --转自唐巧
- linux---------------centos6.4安装完了以后敲ifconfig,没有局域网ip。解决如下
- 安装JDK,Python SDK及环境变量的配置
- 《利用Python进行数据分析》第4章学习笔记
- vs 2013 编译zlib
- redis来共享各个服务器的session,并同时通过redis来缓存一些常用的资源,加快用户获得请求资源的速度(转)
- ImageLoader的Jar包加载图片
- 【Linux Tips】登陆,提示符,别名
- 小白的Python之路 day5 time,datatime模块详解
- pycaffe︱caffe中fine-tuning模型三重天(函数详解、框架简述)
- python实例一
- fiddler抓包App数据
- Newtonsoft.Json WindowPhone7.1
- TFS2018 连接 K8S集群的方法
- Resource interpreted as Stylesheet but transferred with MIME type text/plain
- android 模仿今日头条ViewPager+TabLayout
- .Net Mvc5Filter与权限认证扩展
- Java获取中文拼音、中文首字母缩写和中文首字母