第4章 Selenium2-java WebDriver API (二)
4.8 定位一组元素
定位一组元素的方法与定位单个元素的方法类似,唯一的区别是在单词element后面多了一个s表示复数。定位一组元素一般用于以下场景:
·批量操作元素,例如勾选页面上所有的复选框。
·先获取一组元素,再从这组对象中过滤出需要操作的元素。例如定位出页面上所有的checkbox,然后选择其中的一个进行操作。
eg:
编写一个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.main.css" rel="stylesheet"/>
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.main.js"></script>
<style>
.well{
width:200px;
height:300px;
margin:40 auto;
border:1px solid red;
}
.control-group{
margin-top:30px;
}
.controls{
margin-left:100px;
margin-top:-15px;
}
</style> </head>
<body> <div class="well">
<h3>checkbox</h3>
<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">checkbox1</label>
<div class="controls">
<input type="checkbox" id="c2"/>
</div>
</div> <div class="control-group">
<label class="control-label" for="c3">checkbox1</label>
<div class="controls">
<input type="checkbox" id="c3"/>
</div>
</div>
</form>
</div>
</body>
</html>
java代码;
package com.cy.selenium; import java.io.File;
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 Test03 {
public static void main(String[] args) throws InterruptedException {
System.out.println("start");
WebDriver driver = new FirefoxDriver();
File file = new File("C:/Users/Administrator/Desktop/test.html");
String filePath = file.getAbsolutePath();
driver.get(filePath);
// 通过css查找一组元素
List<WebElement> inputs = driver.findElements(By.cssSelector("input[type=checkbox]"));
for (WebElement checkbox : inputs) {
checkbox.click();
}
Thread.sleep(3000);
// 刷新
driver.navigate().refresh(); // 选择最后一个checkbox 通过xpath查找一组元素
List<WebElement> checkboxs = driver.findElements(By.xpath(".//input[@type='checkbox']"));
checkboxs.get(checkboxs.size()-1).click();
Thread.sleep(2000);
driver.close(); }
}
效果:
4.9 多表单切换
<html>
<head>
</head>
<body>
<div class="row_fluid">
<div class="span10 well">
<h3>frame</h3>
<iframe id="if" name="nf" src="http://www.baidu.com" width="1200px" height="300px"></iframe>
</div>
</div>
</body>
</html>
java代码:
package com.cy.selenium; import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; public class Iframe { public static void main(String[] args) throws InterruptedException {
System.out.println("start");
WebDriver driver = new FirefoxDriver();
File file = new File("C:/Users/Administrator/Desktop/frame.html");
String filePath = file.getAbsolutePath();
driver.get(filePath); // 切换到iframe (id="if")
driver.switchTo().frame("if");
driver.findElement(By.id("kw")).sendKeys("webDrive");
driver.findElement(By.id("su")).click();
Thread.sleep(4000);
driver.close();
//退回上一级表单
// driver.switchTo().defaultContent(); } }
4.10 多窗口切换
4.11 警告框处理
在WebDriver中处理JavaScript所生成的alert、confirm以及prompt十分简单,具体做法是使用 switch_to_alert()方法定位到 alert/confirm/prompt,然后使用text/accept/dismiss/ sendKeys等方法进行操作。
·getText():返回 alert/confirm/prompt 中的文字信息。
·accept(): 接受现有警告框。
·dismiss():解散现有警告框。
·sendKeys(keysToSend): 发送文本至警告框。keysToSend:将文本发送至警告框。
eg:
package com.cy.selenium; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions; public class Alert {
public static void main(String[] args) throws InterruptedException {
System.out.println("start selenium");
WebDriver driver=new FirefoxDriver();// 用WebDriver new Firefox浏览器的驱动给变量driver,相当于driver拿到了Firefox浏览器的控制权。
driver.get("http://www.baidu.com/");
Actions action=new Actions(driver);
// 悬停在设置上
action.clickAndHold(driver.findElement(By.linkText("设置"))).perform();
// 打开搜索设置
driver.findElement(By.className("setpref")).click();
Thread.sleep(3000);
//保存设置
driver.findElement(By.className("prefpanelgo")).click();
Thread.sleep(2000);
// 接受弹框
driver.switchTo().alert().accept(); Thread.sleep(2000); driver.close(); }
}
第4章 Selenium2-java WebDriver API (二)的更多相关文章
- python+selenium自动化软件测试(第2章):WebDriver API
2.1 操作元素基本方法 前言前面已经把环境搭建好了,从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可 ...
- selenium2.0(WebDriver) API
1.1 下载selenium2.0的包 官方download包地址:http://code.google.com/p/selenium/downloads/list 官方User Guide: h ...
- 《Java从入门到失业》第二章:Java环境(二):JDK、JRE、JVM
2.2JDK.JRE.JVM 在JDK的安装目录中,我们发现有一个目录jre(其实如果是下一步下一步安装的,在和JDK安装目录同级目录下,还会有一个jre目录).初学Java的同学,有时候搞不清楚这3 ...
- Webdriver API (二)
(转载) 1.3 打开测试页面 对页面对测试,首先要打开被测试页面的地址(如:http://www.google.com),web driver 提供的get方法可以打开一个页面: // And no ...
- selenium2(WebDriver) API
selenium2(WebDriver) API 作者:Glen.He出处:http://www.cnblogs.com/puresoul/ 1.1 下载selenium2.0的包 官方downl ...
- Selenium2+Python:Webdriver API速记手册
由于web自动化常常需要控制浏览器行为和操作页面元素,相关函数又比较多,于是再此记下一份Webdriver API查阅文档以备不时之需. 参考:虫师<Selenium2自动化测试实战>,和 ...
- java JDK8 学习笔记——第11章 线程和并行API
第11章 线程与并行API 11.1 线程 11.1.1 线程 在java中,如果想在main()以外独立设计流程,可以撰写类操作java.lang.Runnable接口,流程的进入点是操作在run( ...
- [selenium webdriver Java]常用api
1. 获取元素文本 WebElement类的getText()方法返回元素的innerText属性.所以元素里如果有子节点一样也会被返回出来.如下所示 public class GetText { @ ...
- java基础(二章)
java基础(二章) 一,变量 1.变量是内存中的一个标识符号,用于存储数据 2.变量命名规则 l 必须以字母.下划线 _ .美元符号 $ 开头 l 变量中,可以包括数字 l 变量中,不能出现特 ...
随机推荐
- VS中程序包错误,引用错误该如何解决
1.找到包的文件.packages.config 对应于: 2.删除掉 packages.config 报错的项.然后再重新添加一次.就没有解决的不了的问题. 是不是很爽.....
- HTML第二篇
1>压缩文件格式:使用.zip格式较好 2>charset(字符集) 国内最新字符集格式为:gb18030 国际上通用的字符集是:UTF-8 3>添加图片 <img sr ...
- window 安装mysql
常见错误:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 密码输入错误:无法远程 ...
- java多线程系列6 synchronized 加强版 ReentrantLock
ReentrantLock类是可重入.互斥.实现了Lock接口的锁,它与使用synchronized方法和快具有相同的基本行为和语义,并且扩展了其能力.ReenreantLock类的常用方法有: Re ...
- js将数组根据条件分组
//将数组根据条件分组 function getTreeDateByParam(list, param, fun){ var data = {}; if(list && list.le ...
- Chrome书签添加到百度网盘
一:Chrome是最干净的浏览器了,但是无奈国内的环境导致书签不方便保存到云端,如果保存到本地那么就要经常自己备份之类的: 二:由以上的需求背景终于找到了可以将chrome打开的网页保存到百度网盘里[ ...
- Apache Thrift的C++多线程编程定式
Facebook贡献给Apache的开源RPC组件Thrift有着广泛的应用,C++中使用Thrift也十分普遍,但由于Thrift的Handler会被多个线程调用,因而多线程中应用并不直接的友好,利 ...
- 2017-2018-1 20155326信息安全系统设计基础》嵌入式C语言课上考试补交
2017-2018-1 20155326信息安全系统设计基础>嵌入式C语言课上考试补交 PPT上的例子 已知位运算规则为: &0 --> 清零 &1 --> 不变 | ...
- WCF双工通信单工通信
1.单工模式 单向通信,指通信只有一个方向进行,即从客户端流向服务,服务不会发送响应,而客户端也不会期望会有响应.这种情况下,客户端发送消息,然后继续执行 运行后报错: 2.双工模式 双工模式的特点是 ...
- 进程控制(Note for apue and csapp)
1. Introduction We now turn to the process control provided by the UNIX System. This includes the cr ...