三十一、使用页面的文字内容识别和处理新弹出的浏览器窗口

被测试网页的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源码:

frameset.html
frame_left.html
frame_middle.html
frame_right.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

在frame_left.html同级目录下新增iframe.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 实例详解(四)的更多相关文章

  1. WebDriver API 实例详解(三)

    二十一.模拟鼠标右键事件 被测试网页的网址: http://www.sogou.com Java语言版本的API实例代码: package test; import org.testng.annota ...

  2. WebDriver API 实例详解(二)

    十一.双击某个元素 被测试网页的html源码: <html> <head> <meta charset="UTF-8"> </head&g ...

  3. WebDriver API 实例详解(一)

    一.访问某网页地址 被测试网页的网址: http://www.baidu.com Java语言版本的API实例代码: 方法1: package test; import org.testng.anno ...

  4. 官网实例详解-目录和实例简介-keras学习笔记四

    官网实例详解-目录和实例简介-keras学习笔记四 2018-06-11 10:36:18 wyx100 阅读数 4193更多 分类专栏: 人工智能 python 深度学习 keras   版权声明: ...

  5. 《HTML5网页开发实例详解》连载(四)HTML5中的FileSystem接口

    HTML 5除了提供用于获取文件信息的File对象外,还添加了FileSystem相关的应用接口.FileSystem对于不同的处理功能做了细致的分类,如用于文件读取和处理的FileReader和Fi ...

  6. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

  7. linux基础-磁盘阵列(RAID)实例详解

    磁盘阵列(RAID)实例详解 raid技术分类 软raid技术 硬raid技术 Raid和lvm的区别 为什么选择用raid RAID详解 RAID-0 RAID-1 RAID-5 Raid-10 R ...

  8. Cocos2d-x 3.X手游开发实例详解

    Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...

  9. JavaScript学习笔记-实例详解-类(一)

    实例详解-类(一): //每个javascript函数(除了bind())都自动拥有一个prototype对象// 在未添加属性或重写prototype对象之前,它只包含唯一一个不可枚举属性const ...

随机推荐

  1. 超全面的JavaWeb笔记day15<mysql数据库>

    1.数据库的概述 2.SQL 3.DDL 4.DML 5.DCL 6.DQL MySQL 数据库 1 数据库概念(了解) 1.1 什么是数据库 数据库就是用来存储和管理数据的仓库! 数据库存储数据的优 ...

  2. Html解析

    相关解析组件: HtmlAgilityPack CsQuery Winista.Text.HtmlParser

  3. python2.0_day20_bbs系统开发

    BBS是一个最简单的项目.在我们把本节课程的代码手敲一遍后,算是实战项目有一个入门.首先一个项目的第一步是完成表设计,在没有完成表结构设计之前,千万不要动手开发(这是老司机的忠告!)废话不多说,现在我 ...

  4. Apache nutch1.5 & Apache solr3.6

    第1章引言 1.1nutch和solr Nutch 是一个开源的.Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具. Solr 拥有像 web-services API 的独立的 ...

  5. Spring学习笔记--声明一个简单的Bean

    spring依赖的maven dependencyhttp://mvnrepository.com/artifact/org.springframework 在pom.xml中添加如下依赖: < ...

  6. 宏(define)与常量(const)

    http://mp.weixin.qq.com/s?__biz=MzAxMzE2Mjc2Ng==&mid=402470669&idx=1&sn=e34db91190d8d46f ...

  7. java基础---->数字签名算法的介绍

    数字签名(又称公钥数字签名)是一种类似写在纸上的普通的物理签名,但是使用了公钥加密领域的技术实现,用于鉴别数字信息的方法.关于数字签名的介绍,可以参见百度百科:http://baike.baidu.c ...

  8. spring aop 样例

    基于注解的AOP 方式 1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver- ...

  9. Python汉英/英汉翻译(百度API/有道API)

    一.百度API实现 Step1:申请API Key 以前用过BAE,已经有了Api Key,没有的可以去申请 Step2:挺简单,直接看实现的代码吧 ```python #coding:utf-8 i ...

  10. TextureMerger1.6.6 三:Bitmap Font的制作和使用

    BitmapFont主要用于特殊字体在游戏中的使用. 比如我想使用方正剪纸字体,但是没加载方正剪纸.ttf字体时,egret是没法使用这种字体的. 或者美工制作了效果拔群的0123456789数字字体 ...