我有一个实用方法来切换到所需的窗口,如下所示
public class Utility
{
public static WebDriver getHandleToWindow(String title){
//parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
System.err.println("No of windows : " + windowIterator.size());
for (String s : windowIterator) {
String windowHandle = s;
popup = WebDriverInitialize.getDriver().switchTo().window(windowHandle);
System.out.println("Window Title : " + popup.getTitle());
System.out.println("Window Url : " + popup.getCurrentUrl());
if (popup.getTitle().equals(title) ){
System.out.println("Selected Window Title : " + popup.getTitle());
return popup;
}
}
System.out.println("Window Title :" + popup.getTitle());
System.out.println();
return popup;
}
}
一旦窗口的标题作为参数传递,它将带您到所需的窗口。在你的情况下,你可以做到。
Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");
然后再次使用相同的方法切换到父窗口
Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");
此方法在处理多个窗口时有效。