• selenium webdriver学习(六)------------如何得到弹出窗口


    selenium webdriver学习(六)------------如何得到弹出窗口

    在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id、name的时候。当时还整理了处理了几种方法,详见:http://seleniumcn.cn/read.php?tid=791 。在selenium webdriver中得到新开窗口相对简单的多,它无关新开窗口的id、name等属性。以下面的html为例:

    1. <span style="white-space: normal; background-color: rgb(255, 255, 255); ">test.html</span>  
    2.   
    3.   
    4. <html>  
    5.    
    6.     <head><title>Test Popup Window</title></head>  
    7.    
    8.     <body>  
    9.    
    10.         <id = "51" href = "http://www.51.com/" target = "_blank">Let's go!</a>  
    11.    
    12.     </body>  
    13.    
    14. </html>  

      下面的代码演示了如何去得到弹出的新窗口

    1. import java.util.Iterator;  
    2. import java.util.Set;  
    3.   
    4. import org.openqa.selenium.By;  
    5. import org.openqa.selenium.WebDriver;  
    6. import org.openqa.selenium.firefox.FirefoxDriver;  
    7.   
    8. public class PopupWindowTest {  
    9.   
    10.   
    11.     /** 
    12.      * @author gongjf 
    13.      */  
    14.     public static void main(String[] args) {  
    15.         System.setProperty("webdriver.firefox.bin","D:\Program Files\Mozilla Firefox\firefox.exe");    
    16.         WebDriver dr = new FirefoxDriver();  
    17.         String url ="\Your\Path\to\main.html";  
    18.         dr.get(url);      
    19.         dr.findElement(By.id("51")).click();  
    20.         //得到当前窗口的句柄  
    21.         String currentWindow = dr.getWindowHandle();  
    22.         //得到所有窗口的句柄  
    23.         Set<String> handles = dr.getWindowHandles();  
    24.         Iterator<String> it = handles.iterator();  
    25.         while(it.hasNext()){  
    26.             String handle = it.next();  
    27.             if(currentWindow.equals(handle)) continue;  
    28.             WebDriver window = dr.switchTo().window(handle);  
    29.             System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());  
    30.         }  
    31.     }  
    32.   
    33. }  

     输出结果:

    1. title,url = 51.com 真人配对玩游戏,http://www.51.com/  

     

    捕获或者说定位弹出窗口的关键在于获得弹出窗口的句柄。(句柄,我的理解是浏览器窗口的一个唯一标识,记得以前玩"按键精灵"也有这玩样。)

    在上面的代码里,使用windowhandle方法来获取当前浏览器窗口的句柄,使用了windowhandles方法获取所有弹出的浏览器窗口的句柄,然后通过排除当前句柄的方法来得到新开窗口的句柄。

    在获取新弹出窗口的句柄后,使用switchto.window(newwindow_handle)方法,将新窗口的句柄作为参数传入既可捕获到新窗口了。

    如果想回到以前的窗口定位元素,那么再调用1次switch_to.window方法,传入之前窗口的句柄既可达到目的。

    ----------------------------------------------------------2102年6月20日------------------------------------------------------

    PS:今天发现while里的代码有些问题。由原来的:

    1. while(it.hasNext()){  
    2.             if(currentWindow ==  it.next()) continue;  
    3.             WebDriver   window = dr.switchTo().window(it.next());  
    4.             System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());  
    5.         }  

     

    更改为:

    1. while(it.hasNext()){  
    2.             String handle = it.next();  
    3.             if(currentWindow.equals(handle)) continue;  
    4.             WebDriver   window = dr.switchTo().window(handle);  
    5.             System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());  
    6.         }  

    更改原因: 

    循环里面有两次it.next,多取了一次。

    记下自己的成长笔记,积跬步以成千里。知识额无穷尽矣!--Alter
  • 相关阅读:
    Qt进程间通信
    reinterpret
    vs调试技巧
    利用QSystemSemaphore和QSharedMemory实现进程间通讯
    QLocalSocket
    QShareMemory
    qt动态库实现无边框窗体的消息处理 nativeEvent的使用
    BCB6常用快捷键
    1219个人总结
    冲刺二 12.6
  • 原文地址:https://www.cnblogs.com/dayiran1222/p/5001594.html
Copyright © 2020-2023  润新知