我打印时为什么要让父窗口处理程序和子窗​​口处理程序相同?

时间:2018-02-10 15:09:34

标签: selenium selenium-webdriver iterator window-handles

String parentWindowHandler = browser.getWindowHandle(); // Store your parent window
System.out.println(parentWindowHandler);

browser.findElement(By.cssSelector("button.btn-danger")).click();
String subWindowHandler = null;

Set<String> handles = browser.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
    subWindowHandler = iterator.next();
}
browser.switchTo().window(subWindowHandler);
System.out.println(subWindowHandler);// switch to popup window

1 个答案:

答案 0 :(得分:-1)

集合顶部的迭代器。当您到iterator.next()时,它将只是您的父窗口。如果您想要获取子窗口句柄,请执行更多iterator.next()

while (iterator.hasNext()){
parentWindowHandler = iterator.next();
subWindowHandler = iterator.next();

}