selenium webdriver学习 06 – 怎么得到弹出窗口

玩技站长 Auto测试评论137字数 2296阅读模式
在selenium 1.X里面得到弹出窗口是一件比较麻烦的事,特别是新开窗口没有id、name的时候。当时还整理了处理了几种方法,详 见:http://seleniumcn.cn/read.php?tid=791 。在selenium webdriver中得到新开窗口相对简单的多,它无关新开窗口的id、name等属性。以下面的html为例:
<span style="white-space: normal; background-color: rgb(255, 255, 255); ">test.html</span>
<html>
<head><title>Test Popup Window</title></head>
<body>
<a id = "51" href = "http://www.51.com/" target = "_blank">Let's go!</a>
</body>
</html>
下面的代码演示了如何去得到弹出的新窗口
文章源自玩技e族-https://www.playezu.com/10902.html
import java.util.Iterator;  
import java.util.Set;  
import org.openqa.selenium.By;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;  
 
public class PopupWindowTest {  
    /**
     * @author gongjf
     */  
    public static void main(String[] args) {  
        System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");    
        WebDriver dr = new FirefoxDriver();  
        String url ="\\Your\\Path\\to\\main.html";  
        dr.get(url);      
        dr.findElement(By.id("51")).click();  
        //得到当前窗口的句柄  
        String currentWindow = dr.getWindowHandle();  
        //得到所有窗口的句柄  
        Set<String> handles = dr.getWindowHandles();  
        Iterator<String> it = handles.iterator();  
        while(it.hasNext()){  
            String handle = it.next();  
            if(currentWindow.equals(handle)) continue;  
            WebDriver window = dr.switchTo().window(handle);  
            System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());  
        }  
    }  
}
 输出结果:
title,url = 51.com 真人配对玩游戏,http://www.51.com/
 捕获或者说定位弹出窗口的关键在于获得弹出窗口的句柄。(句柄,我的理解是浏览器窗口的一个唯一标识,记得以前玩"按键精灵"也有这玩样。)
在上面的代码里,使用windowhandle方法来获取当前浏览器窗口的句柄,使用了windowhandles方法获取所有弹出的浏览器窗口的句柄,然后通过排除当前句柄的方法来得到新开窗口的句柄。
在获取新弹出窗口的句柄后,使用switchto.window(newwindow_handle)方法,将新窗口的句柄作为参数传入既可捕获到新窗口了。
如果想回到以前的窗口定位元素,那么再调用1次switch_to.window方法,传入之前窗口的句柄既可达到目的。
----------------------------------------------------------2102年6月20日------------------------------------------------------
PS:今天发现while里的代码有些问题。由原来的:
文章源自玩技e族-https://www.playezu.com/10902.html
while(it.hasNext()){  
            if(currentWindow ==  it.next()) continue;  
            WebDriver   window = dr.switchTo().window(it.next());  
            System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());  
        }  
 
更改为:
 
while(it.hasNext()){  
            String handle = it.next();  
            if(currentWindow.equals(handle)) continue;  
            WebDriver   window = dr.switchTo().window(handle);  
            System.out.println("title,url = "+window.getTitle()+","+window.getCurrentUrl());  
        }
更改原因:
循环里面有两次it.next,多取了一次。

文章源自玩技e族-https://www.playezu.com/10902.html 文章源自玩技e族-https://www.playezu.com/10902.html

 
  • 版权提示:本站仅供存储任何法律责任由作者承担▷诈骗举报◁▷新闻不符◁▷我要投稿◁
    风险通知:非原创文章均为网络投稿真实性无法判断,侵权联系2523030730
    免责声明:内容来自用户上传发布或新闻客户端自媒体,切勿!切勿!切勿!添加联系方式以免受骗。
  • 原创转载:https://www.playezu.com/10902.html
    转载说明: 点我前往阅读>>>
匿名

发表评论

匿名网友
确定