WebDriver API 实例详解(四)
三十一、使用页面的文字内容识别和处理新弹出的浏览器窗口
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<body>
<p id="p1">你爱吃的水果么?</p>
<br><br>
<a href="http://www.sogou.com" target="_blank">sogou搜索</a>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File;
import java.util.Set; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file12.html";
driver.get(url);
String parentWindowHandle = driver.getWindowHandle();
WebElement sogouLink = driver.findElement(By.xpath("//a"));
sogouLink.click();
Set<String> allWindowsHandles = driver.getWindowHandles();
if(!allWindowsHandles.isEmpty()){
for(String windowHandle:allWindowsHandles){
try {
if(driver.switchTo().window(windowHandle).getPageSource().contains("搜狗搜索")){
driver.findElement(By.id("query")).sendKeys("sogou");
}
} catch (NoSuchWindowException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
driver.switchTo().window(parentWindowHandle);
Assert.assertEquals(driver.getTitle(), "你喜欢的水果");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
三十二、操作JavaScript的Alert弹窗
目的:能够模拟单击弹出的Alert窗口的-‘确定’-按钮
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<body>
<input id="button" type="button" onclick="alert('这是一个alert弹出框');" value="单击此按钮"></input>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File;
import java.util.Set; import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file13.html";
driver.get(url);
WebElement button = driver.findElement(By.xpath("//input"));
button.click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Alert alert = driver.switchTo().alert();
Assert.assertEquals("这是一个alert弹出框", alert.getText());
alert.accept();//单击确定按钮
} catch (NoAlertPresentException e) {
// TODO: handle exception
Assert.fail("alert未被找到");
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
三十三、操作JavaScript的confirm弹窗
目的:能够模拟单击JavaScript弹出的confirm框中的“确定”和“取消”按钮。
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<body>
<input id="button" type="button" onclick="confirm('这是一个confirm弹出框');" value="单击此按钮"></input>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file14.html";
driver.get(url);
WebElement button = driver.findElement(By.xpath("//input"));
button.click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Alert alert = driver.switchTo().alert();
Assert.assertEquals("这是一个confirm弹出框", alert.getText());
//alert.accept();//单击确定按钮
alert.dismiss();//单击取消
} catch (NoAlertPresentException e) {
// TODO: handle exception
Assert.fail("confirm未被找到");
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
三十四、操作JavaScript的prompt弹窗
目的:能够在JavaScript的prompt弹窗中输入自定义的字符串,单击“确定”按钮和“取消”按钮。
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<body>
<input id="button" type="button" onclick="prompt('这是一个prompt弹出框');" value="单击此按钮"></input>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file15.html";
driver.get(url);
WebElement button = driver.findElement(By.xpath("//input"));
button.click();
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Alert alert = driver.switchTo().alert();
Assert.assertEquals("这是一个prompt弹出框", alert.getText());
alert.sendKeys("想改变命运,就必须每天学习2小时");
Thread.sleep(3000);
alert.accept();//单击确定按钮
//alert.dismiss();//单击取消
} catch (NoAlertPresentException e) {
// TODO: handle exception
Assert.fail("confirm未被找到");
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
三十五、操作Frame中的页面元素
目的:能够进入页面的不同Frame中进行页面元素的操作。
被测试网页的HTML源码:
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "frameset.html";
driver.get(url);
driver.switchTo().frame("leftframe");
WebElement leftframeText = driver.findElement(By.xpath("//p"));
Assert.assertEquals("这是左侧frame页面上的文字", leftframeText.getText());
driver.switchTo().defaultContent();
//
driver.switchTo().frame("middleframe");
WebElement middleframeText = driver.findElement(By.xpath("//p"));
Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText());
driver.switchTo().defaultContent();
//
driver.switchTo().frame("rightframe");
WebElement rightframeText = driver.findElement(By.xpath("//p"));
Assert.assertEquals("这是右侧frame页面上的文字", rightframeText.getText());
driver.switchTo().defaultContent();
//
//使用索引。从0开始
driver.switchTo().frame(1);
middleframeText = driver.findElement(By.xpath("//P"));
Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText()); try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
三十六、使用Frame中的HTML源码内容来操作Frame
目的:能够使用Frame页面的HTML源码定位指定的Frame页面并进行操作。
被测试网页的HTML源码:
同三十五被测试页面的HTML源码。
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; 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.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "frameset.html";
driver.get(url);
List<WebElement> frames = driver.findElements(By.tagName("frame"));
for(WebElement frame:frames){
driver.switchTo().frame(frame);
if(driver.getPageSource().contains("中间frame")){
WebElement middleframeText = driver.findElement(By.xpath("//p"));
Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText());
break;
}else{
driver.switchTo().defaultContent();
}
}
driver.switchTo().defaultContent();
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
三十七、操作IFrame中的页面元素
被测试网页的HTML源码:
同三十五被测试页面的HTML源码,只是需要更新如下页面的HTML源码:
修改frame_left.html源码:
在frame_left.html同级目录下新增iframe.html文件:
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "frameset.html";
driver.get(url);
driver.switchTo().frame("leftframe");
WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe);
WebElement p = driver.findElement(By.xpath("//p"));
Assert.assertEquals("这是iframe页面上的文字", p.getText());
driver.switchTo().defaultContent();
driver.switchTo().frame("middleframe");
try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
三十八、操作浏览器的Cookie
目的:能够遍历输出所有的Cookie的Key和value;能够删除指定的Cookie对象;能够删除所有的Cookie对象。
被测试网页的地址:
http:www.sogou.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod;
import java.util.Set;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.sogou.com";
@Test
public void opentest() {
driver.get(url);
Set<Cookie> cookies = driver.manage().getCookies();
Cookie newCookie = new Cookie("cookieName","cookieValue");
System.out.println(String.format("Domain-> name -> value -> expiry -> path"));
for(Cookie cookie:cookies){
System.out.println(String.format("%s-> %s -> %s -> %s -> %s",
cookie.getDomain(),cookie.getName(),
cookie.getValue(),cookie.getExpiry(),cookie.getPath()));
}
//删除cookie的3种方法
//
driver.manage().deleteCookieNamed("CookieName"); //
driver.manage().deleteCookie(newCookie); //
driver.manage().deleteAllCookies(); try {
Thread.sleep(3000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
WebDriver API 实例详解(四)的更多相关文章
- WebDriver API 实例详解(三)
二十一.模拟鼠标右键事件 被测试网页的网址: http://www.sogou.com Java语言版本的API实例代码: package test; import org.testng.annota ...
- WebDriver API 实例详解(二)
十一.双击某个元素 被测试网页的html源码: <html> <head> <meta charset="UTF-8"> </head&g ...
- WebDriver API 实例详解(一)
一.访问某网页地址 被测试网页的网址: http://www.baidu.com Java语言版本的API实例代码: 方法1: package test; import org.testng.anno ...
- 官网实例详解-目录和实例简介-keras学习笔记四
官网实例详解-目录和实例简介-keras学习笔记四 2018-06-11 10:36:18 wyx100 阅读数 4193更多 分类专栏: 人工智能 python 深度学习 keras 版权声明: ...
- 《HTML5网页开发实例详解》连载(四)HTML5中的FileSystem接口
HTML 5除了提供用于获取文件信息的File对象外,还添加了FileSystem相关的应用接口.FileSystem对于不同的处理功能做了细致的分类,如用于文件读取和处理的FileReader和Fi ...
- (转载)实例详解Android快速开发工具类总结
实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...
- linux基础-磁盘阵列(RAID)实例详解
磁盘阵列(RAID)实例详解 raid技术分类 软raid技术 硬raid技术 Raid和lvm的区别 为什么选择用raid RAID详解 RAID-0 RAID-1 RAID-5 Raid-10 R ...
- Cocos2d-x 3.X手游开发实例详解
Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...
- JavaScript学习笔记-实例详解-类(一)
实例详解-类(一): //每个javascript函数(除了bind())都自动拥有一个prototype对象// 在未添加属性或重写prototype对象之前,它只包含唯一一个不可枚举属性const ...
随机推荐
- VS 最近打开清理bat
VS2008RecentCleaner.bat @echo off @REG Delete HKCU\Software\Microsoft\VisualStudio\9.0\FileMRUList / ...
- HTML 之前未接触过的标签
用于表单的HTML标签 HTML <fieldset> 标签 定义和用法 fieldset 元素可将表单内的相关元素分组. <fieldset> 标签将 ...
- 百度地图api ak值
http://api.map.baidu.com/geocoder/v2/?ak=859d16285fd000feec89e9032513f8bb&callback=renderReverse ...
- gevent动态随时添加任务
关于爬虫,有scrapy框架,也有requests加协程 协程 进程的方法. 相关的包很多,比如threading .threadpool.multiprocessing,还有threadpoolex ...
- [转]ASP.NET MVC 5 - 给电影表和模型添加新字段
在本节中,您将使用Entity Framework Code First来实现模型类上的操作.从而使得这些操作和变更,可以应用到数据库中. 默认情况下,就像您在之前的教程中所作的那样,使用 Entit ...
- ssh通过密钥进行连接
sshd服务提供两种安全验证的方法: 基于口令的安全验证:经过验证帐号与密码即可登陆到远程主机. 基于密钥的安全验证:需要在本地生成"密钥对"后将公钥传送至服务端,进行公共密钥的比 ...
- oracle查锁表
查锁表 select LOCK_INFO.OWNER || '.' || LOCK_INFO.OBJ_NAME as OBJ_NAME, -- 对象名称(已经被锁住) LOCK_INFO.SUBOBJ ...
- android 仿QQ手机版
千人2群开启,欢迎大家围观打酱油,群号145667827 您当前位置 : JavaApk-安卓应用游戏源码服务专家 » QQ » Android项目源码界面超级华丽的仿QQ最新版本 Andro ...
- 【CSS系列】块级元素和行内元素
块级元素: 块级元素生成一个元素框,默认会填充其父元素的内容区,旁边不能有其他元素,换句话说,它在元素框之前和之后生成了“分隔符”. 列表项是块级额元素的一个特例,除了表现方式与其他块元素一致,列表项 ...
- LeetCode——Peeking Iterator
Description: Given an Iterator class interface with methods: next() and hasNext(), design and implem ...