selenium测试(Java)--关闭窗口(二十)

quit方法:退出相关的驱动程序和关闭所有窗口

close方法:关闭当前窗口

 1 package com.test.closewindow;
 2 
 3 import java.util.Iterator;
 4 import java.util.Set;
 5 
 6 import org.openqa.selenium.By;
 7 import org.openqa.selenium.JavascriptExecutor;
 8 import org.openqa.selenium.WebDriver;
 9 import org.openqa.selenium.firefox.FirefoxDriver;
10 
11 public class WindowsClose {
12 
13     public static void main(String[] args) {
14 
15         WebDriver driver = new FirefoxDriver();
16         driver.get("http://www.baidu.com");
17         driver.manage().window().maximize();
18 
19         String searchHandle = driver.getWindowHandle();
20         String newsHandle = null;
21         System.out.println("baidu search handle : " + searchHandle);
22 
23         // 获取百度新闻的连接,然后利用js打开一个新的窗口
24         waitTime(3000);
25         String href = driver.findElement(By.cssSelector("[name=tj_trnews]")).getAttribute("href");
26         System.out.println("the link of news is : " + href);
27         JavascriptExecutor executor = (JavascriptExecutor) driver;
28         executor.executeScript("window.open('" + href + "')");
29 
30         waitTime(3000);
31         Set<String> handles = driver.getWindowHandles();
32         Iterator iterator = handles.iterator();
33         Iterator iterator2 = handles.iterator();
34 
35         // 进入百度新闻窗口,并获取title验证
36         waitTime(3000);
37         while (iterator.hasNext()) {
38             String h = (String) iterator.next();
39             if (h != searchHandle) {
40                 driver.switchTo().window(h);
41                 if (driver.getTitle().contains("百度新闻")) {
42                     System.out.println("switch to news page successfully");
43                     newsHandle = driver.getWindowHandle();
44                     break;
45                 } else {
46                     continue;
47                 }
48             }
49 
50         }
51 
52         // 返回百度首页
53         waitTime(3000);
54         /*
55          * 可以直接使用searchHandle来切换,也可以利用遍历来切换
56          * driver.switchTo().window(searchHandle);
57          * if(driver.getTitle().contains("百度一下")){
58          * System.out.println("switch to search page successfully");
59          * driver.findElement(By.cssSelector("#kw")).sendKeys(
60          * "switch successfully"); }
61          */
62         while (iterator2.hasNext()) {
63             String h = (String) iterator2.next();
64             if (searchHandle.equals(h)) {
65                 driver.switchTo().window(h);
66                 if (driver.getTitle().contains("百度一下")) {
67                     System.out.println("switch to search page successfully");
68                     driver.findElement(By.cssSelector("#kw")).sendKeys("switch successfully");
69                     driver.close();// 只关闭百度首页
70                     System.out.println("close search page successfully");
71                     driver.switchTo().window(newsHandle);// 切换到百度新闻
72                     System.out.println("当前的title是: " + driver.getTitle());// 获取title
73                     waitTime(3000);
74                     break;
75                 } else {
76                     continue;
77                 }
78             }
79 
80         }
81 
82         waitTime(3000);
83         driver.quit();
84 
85     }
86 
87     static public void waitTime(int time) {
88 
89         try {
90             Thread.sleep(time);
91         } catch (InterruptedException e) {
92             // TODO Auto-generated catch block
93             e.printStackTrace();
94         }
95     }
96 
97 }

执行结果:

 

baidu search handle : {75749f9a-f6b7-4fe6-80ae-dd9e053dd577}
the link of news is : http://news.baidu.com/
switch to news page successfully
switch to search page successfully
close search page successfully
当前的title是: 百度新闻搜索——全球最大的中文新闻平台

    原文作者:java 线程池
    原文地址: http://www.cnblogs.com/moonpool/p/5677807.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞