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

被测试网页的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. C#中汉字轻松得到拼音全文类

    public class chs2py { ,-,-,-,-,-,-,-,-,-,-,-,-,-, -,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-, -,-,-,-,-,-,-,-,- ...

  2. ssh在本地调用远程主机上的命令,不登录远程主机shell

    需求描述: 在实际shell脚本的编写过程中,需要通过ssh远程执行一个命令,并返回执行的结果 简单来说,就是将命令发送到远程的主机上进行执行,但是并没有实际的登录到远程主机上.即通过 ssh的方式本 ...

  3. Spring------Spring boot data jpa的使用方法

    1.DoMain.java import org.springframework.boot.SpringApplication; import org.springframework.boot.aut ...

  4. rac_grid自检提示缺少cvuqdisk包

    原创作品,出自 "深蓝的blog" 博客,欢迎转载.转载时请务必注明下面出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...

  5. java.lang.IncompatibleClassChangeError: Implementing class

    项目中使用了quartz,但是jar包却有两个,一个1.8版本,一个2.1版本,导致jar包冲突,所以导致一启动tomcat就出现: Caused by: java.lang.Incompatible ...

  6. RegisterHotKey注册快捷键

    RegisterHotKey的具体使用方使用方法如下: BOOL   RegisterHotKey( HWND   hWnd,         //响应该热键的窗口句柄 Int   id,       ...

  7. PHP遍历文件夹及子文件夹所有文件(此外还有飞递归的方法)

    <html> <body> <?php function traverse($path = '.') { $current_dir = opendir($path); / ...

  8. c++11——列表初始化

    1. 使用列表初始化 在c++98/03中,对象的初始化方法有很多种,例如 int ar[3] = {1,2,3}; int arr[] = {1,2,3}; //普通数组 struct A{ int ...

  9. C# EMS Client

    从 C# 客户端连接 Tibco EMS 下面例子简要介绍 C# 客户端怎样使用 TIBCO.EMS.dll 来连接 EMS 服务器. using System; using System.Diagn ...

  10. Ubuntu14.04下安装DevStack

    虚拟机中的网络配置 NET8 为nat net2 为host-only 虚拟机网络配置 # The primary network interface vmnet nat type auto eth0 ...