问题
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