如何在selenium上获取所有打开的窗口和浏览器浏览器的处理程序?

时间:2015-08-10 21:16:51

标签: java eclipse selenium browser qa

我正在搜索一种方法,该方法将在所有打开的窗口,标签和浏览器中返回网址,而不仅仅是当前在selenium中运行的网址。我下面的代码只返回通过运行当前测试实例化的当前窗口。

String originalWindowHandle = driver.getWindowHandle();
System.out.println("original Window handles are: " + originalWindowHandle);
System.out.println("Title of original Page:" + driver.getTitle());  
java.util.Set<java.lang.String> windowHandles = driver.getWindowHandles();
System.out.println(windowHandles);
int count = windowHandles.size();
//if (count > 1) {
for (String window: windowHandles) {
        String url = driver.getCurrentUrl();
        System.out.println("current url is: " + url);
    }       
//}

任何想法或线索都会有所帮助,谢谢。

1 个答案:

答案 0 :(得分:3)

你得到了正确的数字。但从未切换到新开的窗户。在增强的for循环中,您希望切换到每个窗口,然后检查URL

String originalWindowHandle = driver.getWindowHandle();
System.out.println("original Window handles are: " + originalWindowHandle);
System.out.println("Title of original Page:" + driver.getTitle());  
java.util.Set<java.lang.String> windowHandles = driver.getWindowHandles();
System.out.println(windowHandles);
int count = windowHandles.size();
//if (count > 1) {
for (String window: windowHandles) {
    if(!window.equals(originalWindowHandle)){
        driver.switchTo().window(window);
        String url = driver.getCurrentUrl();
        System.out.println("current url is: " + url);        
    }  
    //to go back to original    
    driver.switchTo().window(originalWindowHandle); 

}   
//}