WebDriver API 实例详解(四)

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

被测试网页的HTML源码:

 1 <html>
 2 <head>
 3 <meta charset="UTF-8" content="text/html">
 4 <title>你喜欢的水果</title>
 5 </head>
 6 <body>
 7     <p id="p1">你爱吃的水果么?</p>
 8     <br><br>
 9     <a href="http://www.sogou.com" target="_blank">sogou搜索</a>
10 </body>
11 </html>

Java语言版本的API实例代码:

 1 package test;
 2
 3 import org.testng.annotations.Test;
 4
 5 import org.testng.annotations.BeforeMethod;
 6
 7 import java.io.File;
 8 import java.util.Set;
 9
10 import org.openqa.selenium.By;
11 import org.openqa.selenium.NoSuchWindowException;
12 import org.openqa.selenium.WebDriver;
13 import org.openqa.selenium.WebElement;
14 import org.openqa.selenium.chrome.ChromeDriver;
15 import org.testng.Assert;
16 import org.testng.annotations.AfterMethod;
17
18 public class ChormeOpen {
19     WebDriver driver;
20
21     @Test
22     public void opentest() {
23         File file = new File("");
24         String url = file.getAbsolutePath() + "/html/" + "file12.html";
25         driver.get(url);
26         String parentWindowHandle = driver.getWindowHandle();
27         WebElement sogouLink = driver.findElement(By.xpath("//a"));
28         sogouLink.click();
29         Set<String> allWindowsHandles = driver.getWindowHandles();
30         if(!allWindowsHandles.isEmpty()){
31             for(String windowHandle:allWindowsHandles){
32                 try {
33                     if(driver.switchTo().window(windowHandle).getPageSource().contains("搜狗搜索")){
34                         driver.findElement(By.id("query")).sendKeys("sogou");
35                     }
36                 } catch (NoSuchWindowException e) {
37                     // TODO: handle exception
38                     e.printStackTrace();
39                 }
40             }
41         }
42         driver.switchTo().window(parentWindowHandle);
43         Assert.assertEquals(driver.getTitle(), "你喜欢的水果");
44         try {
45             Thread.sleep(3000);
46         } catch (InterruptedException e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }
50     }
51
52   @BeforeMethod
53   public void beforeMethod() {
54       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
55         driver = new ChromeDriver();
56   }
57
58   @AfterMethod
59   public void afterMethod() {
60       driver.quit();
61   }
62
63 }

三十二、操作JavaScript的Alert弹窗

目的:能够模拟单击弹出的Alert窗口的-‘确定’-按钮

被测试网页的HTML源码:

1 <html>
2 <head>
3 <meta charset="UTF-8" content="text/html">
4 <title>你喜欢的水果</title>
5 </head>
6 <body>
7     <input id="button" type="button" onclick="alert(‘这是一个alert弹出框‘);" value="单击此按钮"></input>
8 </body>
9 </html>

Java语言版本的API实例代码:

 1 package test;
 2
 3 import org.testng.annotations.Test;
 4
 5 import org.testng.annotations.BeforeMethod;
 6
 7 import java.io.File;
 8 import java.util.Set;
 9
10 import org.openqa.selenium.Alert;
11 import org.openqa.selenium.By;
12 import org.openqa.selenium.NoAlertPresentException;
13 import org.openqa.selenium.NoSuchWindowException;
14 import org.openqa.selenium.WebDriver;
15 import org.openqa.selenium.WebElement;
16 import org.openqa.selenium.chrome.ChromeDriver;
17 import org.testng.Assert;
18 import org.testng.annotations.AfterMethod;
19
20 public class ChormeOpen {
21     WebDriver driver;
22
23     @Test
24     public void opentest() {
25         File file = new File("");
26         String url = file.getAbsolutePath() + "/html/" + "file13.html";
27         driver.get(url);
28         WebElement button = driver.findElement(By.xpath("//input"));
29         button.click();
30         try {
31             Thread.sleep(3000);
32         } catch (InterruptedException e) {
33             // TODO Auto-generated catch block
34             e.printStackTrace();
35         }
36         try {
37             Alert alert = driver.switchTo().alert();
38             Assert.assertEquals("这是一个alert弹出框", alert.getText());
39             alert.accept();//单击确定按钮
40         } catch (NoAlertPresentException e) {
41             // TODO: handle exception
42             Assert.fail("alert未被找到");
43             e.printStackTrace();
44         }
45         try {
46             Thread.sleep(3000);
47         } catch (InterruptedException e) {
48             // TODO Auto-generated catch block
49             e.printStackTrace();
50         }
51     }
52
53   @BeforeMethod
54   public void beforeMethod() {
55       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
56         driver = new ChromeDriver();
57   }
58
59   @AfterMethod
60   public void afterMethod() {
61       driver.quit();
62   }
63
64 }

三十三、操作JavaScript的confirm弹窗

目的:能够模拟单击JavaScript弹出的confirm框中的“确定”和“取消”按钮。

被测试网页的HTML源码:

1 <html>
2 <head>
3 <meta charset="UTF-8" content="text/html">
4 <title>你喜欢的水果</title>
5 </head>
6 <body>
7     <input id="button" type="button" onclick="confirm(‘这是一个confirm弹出框‘);" value="单击此按钮"></input>
8 </body>
9 </html>

Java语言版本的API实例代码:

 1 package test;
 2
 3 import org.testng.annotations.Test;
 4
 5 import org.testng.annotations.BeforeMethod;
 6
 7 import java.io.File;
 8
 9 import org.openqa.selenium.Alert;
10 import org.openqa.selenium.By;
11 import org.openqa.selenium.NoAlertPresentException;
12 import org.openqa.selenium.WebDriver;
13 import org.openqa.selenium.WebElement;
14 import org.openqa.selenium.chrome.ChromeDriver;
15 import org.testng.Assert;
16 import org.testng.annotations.AfterMethod;
17
18 public class ChormeOpen {
19     WebDriver driver;
20
21     @Test
22     public void opentest() {
23         File file = new File("");
24         String url = file.getAbsolutePath() + "/html/" + "file14.html";
25         driver.get(url);
26         WebElement button = driver.findElement(By.xpath("//input"));
27         button.click();
28         try {
29             Thread.sleep(3000);
30         } catch (InterruptedException e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33         }
34         try {
35             Alert alert = driver.switchTo().alert();
36             Assert.assertEquals("这是一个confirm弹出框", alert.getText());
37             //alert.accept();//单击确定按钮
38             alert.dismiss();//单击取消
39         } catch (NoAlertPresentException e) {
40             // TODO: handle exception
41             Assert.fail("confirm未被找到");
42             e.printStackTrace();
43         }
44         try {
45             Thread.sleep(3000);
46         } catch (InterruptedException e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }
50     }
51
52   @BeforeMethod
53   public void beforeMethod() {
54       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
55         driver = new ChromeDriver();
56   }
57
58   @AfterMethod
59   public void afterMethod() {
60       driver.quit();
61   }
62
63 }

三十四、操作JavaScript的prompt弹窗

目的:能够在JavaScript的prompt弹窗中输入自定义的字符串,单击“确定”按钮和“取消”按钮。

被测试网页的HTML源码:

1 <html>
2 <head>
3 <meta charset="UTF-8" content="text/html">
4 <title>你喜欢的水果</title>
5 </head>
6 <body>
7     <input id="button" type="button" onclick="prompt(‘这是一个prompt弹出框‘);" value="单击此按钮"></input>
8 </body>
9 </html>

Java语言版本的API实例代码:

 1 package test;
 2
 3 import org.testng.annotations.Test;
 4
 5 import org.testng.annotations.BeforeMethod;
 6
 7 import java.io.File;
 8
 9 import org.openqa.selenium.Alert;
10 import org.openqa.selenium.By;
11 import org.openqa.selenium.NoAlertPresentException;
12 import org.openqa.selenium.WebDriver;
13 import org.openqa.selenium.WebElement;
14 import org.openqa.selenium.chrome.ChromeDriver;
15 import org.testng.Assert;
16 import org.testng.annotations.AfterMethod;
17
18 public class ChormeOpen {
19     WebDriver driver;
20
21     @Test
22     public void opentest() {
23         File file = new File("");
24         String url = file.getAbsolutePath() + "/html/" + "file15.html";
25         driver.get(url);
26         WebElement button = driver.findElement(By.xpath("//input"));
27         button.click();
28         try {
29             Thread.sleep(3000);
30         } catch (Exception e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33         }
34         try {
35             Alert alert = driver.switchTo().alert();
36             Assert.assertEquals("这是一个prompt弹出框", alert.getText());
37             alert.sendKeys("想改变命运,就必须每天学习2小时");
38             Thread.sleep(3000);
39             alert.accept();//单击确定按钮
40             //alert.dismiss();//单击取消
41         } catch (NoAlertPresentException e) {
42             // TODO: handle exception
43             Assert.fail("confirm未被找到");
44             e.printStackTrace();
45         }catch (Exception e) {
46             // TODO: handle exception
47             e.printStackTrace();
48         }
49         try {
50             Thread.sleep(3000);
51         } catch (Exception e) {
52             // TODO Auto-generated catch block
53             e.printStackTrace();
54         }
55     }
56
57   @BeforeMethod
58   public void beforeMethod() {
59       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
60         driver = new ChromeDriver();
61   }
62
63   @AfterMethod
64   public void afterMethod() {
65       driver.quit();
66   }
67
68 }

三十五、操作Frame中的页面元素

目的:能够进入页面的不同Frame中进行页面元素的操作。

被测试网页的HTML源码:

frameset.html

frame_left.html

frame_middle.html

frame_right.html

Java语言版本的API实例代码:

 1 package test;
 2
 3 import org.testng.annotations.Test;
 4
 5 import org.testng.annotations.BeforeMethod;
 6
 7 import java.io.File;
 8
 9 import org.openqa.selenium.By;
10 import org.openqa.selenium.WebDriver;
11 import org.openqa.selenium.WebElement;
12 import org.openqa.selenium.chrome.ChromeDriver;
13 import org.testng.Assert;
14 import org.testng.annotations.AfterMethod;
15
16 public class ChormeOpen {
17     WebDriver driver;
18
19     @Test
20     public void opentest() {
21         File file = new File("");
22         String url = file.getAbsolutePath() + "/html/" + "frameset.html";
23         driver.get(url);
24         driver.switchTo().frame("leftframe");
25         WebElement leftframeText = driver.findElement(By.xpath("//p"));
26         Assert.assertEquals("这是左侧frame页面上的文字", leftframeText.getText());
27         driver.switchTo().defaultContent();
28         //
29         driver.switchTo().frame("middleframe");
30         WebElement middleframeText = driver.findElement(By.xpath("//p"));
31         Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText());
32         driver.switchTo().defaultContent();
33         //
34         driver.switchTo().frame("rightframe");
35         WebElement rightframeText = driver.findElement(By.xpath("//p"));
36         Assert.assertEquals("这是右侧frame页面上的文字", rightframeText.getText());
37         driver.switchTo().defaultContent();
38         //
39         //使用索引。从0开始
40         driver.switchTo().frame(1);
41         middleframeText = driver.findElement(By.xpath("//P"));
42         Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText());
43
44         try {
45             Thread.sleep(3000);
46         } catch (Exception e) {
47             // TODO Auto-generated catch block
48             e.printStackTrace();
49         }
50     }
51
52   @BeforeMethod
53   public void beforeMethod() {
54       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
55         driver = new ChromeDriver();
56   }
57
58   @AfterMethod
59   public void afterMethod() {
60       driver.quit();
61   }
62
63 }

三十六、使用Frame中的HTML源码内容来操作Frame

目的:能够使用Frame页面的HTML源码定位指定的Frame页面并进行操作。

被测试网页的HTML源码:

三十五被测试页面的HTML源码。

Java语言版本的API实例代码:

 1 package test;
 2
 3 import org.testng.annotations.Test;
 4
 5 import org.testng.annotations.BeforeMethod;
 6
 7 import java.io.File;
 8 import java.util.List;
 9
10 import org.openqa.selenium.By;
11 import org.openqa.selenium.WebDriver;
12 import org.openqa.selenium.WebElement;
13 import org.openqa.selenium.chrome.ChromeDriver;
14 import org.testng.Assert;
15 import org.testng.annotations.AfterMethod;
16
17 public class ChormeOpen {
18     WebDriver driver;
19
20     @Test
21     public void opentest() {
22         File file = new File("");
23         String url = file.getAbsolutePath() + "/html/" + "frameset.html";
24         driver.get(url);
25         List<WebElement> frames = driver.findElements(By.tagName("frame"));
26         for(WebElement frame:frames){
27             driver.switchTo().frame(frame);
28             if(driver.getPageSource().contains("中间frame")){
29                 WebElement middleframeText = driver.findElement(By.xpath("//p"));
30                 Assert.assertEquals("这是中间frame页面上的文字", middleframeText.getText());
31                 break;
32             }else{
33                 driver.switchTo().defaultContent();
34             }
35         }
36         driver.switchTo().defaultContent();
37         try {
38             Thread.sleep(3000);
39         } catch (Exception e) {
40             // TODO Auto-generated catch block
41             e.printStackTrace();
42         }
43     }
44
45   @BeforeMethod
46   public void beforeMethod() {
47       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
48         driver = new ChromeDriver();
49   }
50
51   @AfterMethod
52   public void afterMethod() {
53       driver.quit();
54   }
55
56 }

三十七、操作IFrame中的页面元素

被测试网页的HTML源码:

三十五被测试页面的HTML源码,只是需要更新如下页面的HTML源码:

修改frame_left.html源码:

frame_left.html

在frame_left.html同级目录下新增iframe.html文件:

iframe.html

Java语言版本的API实例代码:

 1 package test;
 2
 3 import org.testng.annotations.Test;
 4
 5 import org.testng.annotations.BeforeMethod;
 6
 7 import java.io.File;
 8 import org.openqa.selenium.By;
 9 import org.openqa.selenium.WebDriver;
10 import org.openqa.selenium.WebElement;
11 import org.openqa.selenium.chrome.ChromeDriver;
12 import org.testng.Assert;
13 import org.testng.annotations.AfterMethod;
14
15 public class ChormeOpen {
16     WebDriver driver;
17
18     @Test
19     public void opentest() {
20         File file = new File("");
21         String url = file.getAbsolutePath() + "/html/" + "frameset.html";
22         driver.get(url);
23         driver.switchTo().frame("leftframe");
24         WebElement iframe = driver.findElement(By.tagName("iframe"));
25         driver.switchTo().frame(iframe);
26         WebElement p = driver.findElement(By.xpath("//p"));
27         Assert.assertEquals("这是iframe页面上的文字", p.getText());
28         driver.switchTo().defaultContent();
29         driver.switchTo().frame("middleframe");
30         try {
31             Thread.sleep(3000);
32         } catch (Exception e) {
33             // TODO Auto-generated catch block
34             e.printStackTrace();
35         }
36     }
37
38   @BeforeMethod
39   public void beforeMethod() {
40       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
41         driver = new ChromeDriver();
42   }
43
44   @AfterMethod
45   public void afterMethod() {
46       driver.quit();
47   }
48
49 }

三十八、操作浏览器的Cookie

目的:能够遍历输出所有的Cookie的Key和value;能够删除指定的Cookie对象;能够删除所有的Cookie对象。

被测试网页的地址:

http:www.sogou.com

Java语言版本的API实例代码:

 1 package test;
 2
 3 import org.testng.annotations.Test;
 4
 5 import org.testng.annotations.BeforeMethod;
 6 import java.util.Set;
 7 import org.openqa.selenium.Cookie;
 8 import org.openqa.selenium.WebDriver;
 9 import org.openqa.selenium.chrome.ChromeDriver;
10 import org.testng.annotations.AfterMethod;
11
12 public class ChormeOpen {
13     WebDriver driver;
14     String url = "http://www.sogou.com";
15     @Test
16     public void opentest() {
17         driver.get(url);
18         Set<Cookie> cookies = driver.manage().getCookies();
19         Cookie newCookie = new Cookie("cookieName","cookieValue");
20         System.out.println(String.format("Domain-> name -> value -> expiry -> path"));
21         for(Cookie cookie:cookies){
22             System.out.println(String.format("%s-> %s -> %s -> %s -> %s",
23                     cookie.getDomain(),cookie.getName(),
24                     cookie.getValue(),cookie.getExpiry(),cookie.getPath()));
25         }
26         //删除cookie的3种方法
27         //1
28         driver.manage().deleteCookieNamed("CookieName");
29
30         //2
31         driver.manage().deleteCookie(newCookie);
32
33         //3
34         driver.manage().deleteAllCookies();
35
36         try {
37             Thread.sleep(3000);
38         } catch (Exception e) {
39             // TODO Auto-generated catch block
40             e.printStackTrace();
41         }
42     }
43
44   @BeforeMethod
45   public void beforeMethod() {
46       System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
47         driver = new ChromeDriver();
48   }
49
50   @AfterMethod
51   public void afterMethod() {
52       driver.quit();
53   }
54
55 }

时间: 2024-08-29 08:45:09

WebDriver API 实例详解(四)的相关文章

WebDriver API 实例详解(一)

一.访问某网页地址 被测试网页的网址: http://www.baidu.com Java语言版本的API实例代码: 方法1: 1 package test; 2 3 import org.testng.annotations.Test; 4 import org.testng.annotations.BeforeMethod; 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.chrome.ChromeDr

Android基础入门教程——8.3.7 Paint API之—— Xfermode与PorterDuff详解(四)

Android基础入门教程--8.3.7 Paint API之-- Xfermode与PorterDuff详解(四) 标签(空格分隔): Android基础入门教程 本节引言: 上节我们写了关于Xfermode与PorterDuff使用的第一个例子:圆角&圆形图片ImageView的实现, 我们体会到了PorterDuff.Mode.DST_IN给我们带来的好处,本节我们继续来写例子练练手, 还记得Android基础入门教程--8.3.2 绘图类实战示例给大家带来的拔掉美女衣服的实现吗? 当时我

Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(四)

这一章大象将详细分析web层代码,以及使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能.     之前在使用Struts2实现MVC的注解时,是借助struts2-convention这个插件,如今我们使用Spring自带的spring-webmvc组件来实现同样的功能,而且比之以前更简单.另外,还省掉了整合两个框架带来的不稳定因素.     对于Spring MVC框架,我主要讲一下它的常用注解,再结合一些示例进行说明,方便大家能够快速理解.     一.Spring MV

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

HTML 5除了提供用于获取文件信息的File对象外,还添加了FileSystem相关的应用接口.FileSystem对于不同的处理功能做了细致的分类,如用于文件读取和处理的FileReader和FileList对象.用于创建和写入的Blob和FileWriter对象.用于目录和文件系统访问的DirectoryReader和LocalFileSystem对象等,FileSystem功能的出现是浏览器在文件系统上的突破,具有里程碑的意义,虽然目前还尚未完全成熟,但足以让开发者发挥更大的想象空间.

spark2.x由浅入深深到底系列六之RDD java api详解四

学习spark任何的知识点之前,先对spark要有一个正确的理解,可以参考:正确理解spark 本文对join相关的api做了一个解释 SparkConf conf = new SparkConf().setAppName("appName").setMaster("local"); JavaSparkContext sc = new JavaSparkContext(conf); JavaPairRDD<Integer, Integer> javaPa

免费的HTML5连载来了《HTML5网页开发实例详解》连载(四)HTML5中的FileSystem接口

HTML 5除了提供用于获取文件信息的File对象外,还添加了FileSystem相关的应用接口.FileSystem对于不同的处理功能做了细致的分类,如用于文件读取和处理的FileReader和FileList对象.用于创建和写入的Blob和FileWriter对象.用于目录和文件系统访问的DirectoryReader和LocalFileSystem对象等,FileSystem功能的出现是浏览器在文件系统上的突破,具有里程碑的意义,虽然目前还尚未完全成熟,但足以让开发者发挥更大的想象空间.

23、磁盘管理—磁盘阵列(RAID)实例详解

磁盘阵列(RAID)实例详解 raid技术分类 软raid技术 硬raid技术 Raid和lvm的区别 为什么选择用raid RAID详解 RAID-0 RAID-1 RAID-5 Raid-10 Raid的管理 案例:创建一个raid10+冗余盘 磁盘阵列(RAID)实例详解 Raid(磁盘阵列)级别介绍 Raid有"廉价磁盘冗余阵列"的意思,就是利用多块廉价的硬盘组成磁盘组,让数据分部储存在这些硬盘里面,从而达到读取和写入加速的目的:也可以用作数据的冗余,当某块硬盘损毁后,其他硬盘

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

Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰然网创始人杨雍力荐) 于浩洋 著   ISBN 978-7-121-23998-4 2014年9月出版 定价:59.00元 356页 16开 编辑推荐 以Cocos2d-x V3.0为框架全面讲解手游开发的知识和方法 以热门游戏2048.卡牌为例,完整再现手游的开发过程 Cocos2d-x作者之一林

《HTML 5网页开发实例详解》目录

第一篇  从宏观上认识HTML 5 讲述了HTML 5引发的Web革命.HTML 5的整体特性.HTML 5相关概念和框架和开发环境搭建. 第1章 HTML 5引发的Web革命 1.1  你是不是真的了解HTML 5 1.1.1  通过W3C认识HTML 5的发展史 1.1.2  HTML 5.HTML4.XHTML的区别 1.1.3  什么人应该学HTML 5 1.1.4  一个图告诉你如何学习HTML 5 1.2  浏览器之争 1.2.1  说说这些常见的浏览器 1.2.2  浏览器的兼容烦