How to get handlers for all open windows and browsers browsers on selenium?

吃可爱长大的小学妹 提交于 2019-12-11 03:48:46

问题


I am searching for a method that will return the urls and handlers across all open windows, tabs, and browsers, not just the one that is currently being run in selenium. My code below only returns the current window that was instantiated by running the current test.

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);
    }       
//}

any ideas or leads would be helpful, thanks.


回答1:


You are getting correct counts. But never switched to newly opened windows. In enhanced for loop you want to switch to each window and then check 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); 

}   
//}


来源:https://stackoverflow.com/questions/31929347/how-to-get-handlers-for-all-open-windows-and-browsers-browsers-on-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!