WebDriver API 实例详解

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
View Code

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 }
View Code

三十二、操作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>
View Code

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 }
View Code

三十三、操作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>
View Code

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 }
View Code

三十四、操作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>
View Code

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 }
View Code

三十五、操作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 }
View Code

三十六、使用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 }
View Code

三十七、操作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 }
View Code

三十八、操作浏览器的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 }
View Code

 

以上是关于WebDriver API 实例详解的主要内容,如果未能解决你的问题,请参考以下文章

WebDriver高级应用实例

Selenium JavascriptExecutor 详解

Selenium JavascriptExecutor 详解

WebDriver高级应用实例(10)

python+selenium——详解介绍Selenium常用API的使用--python语言(完整版)

Selenium常用API详解介绍