我在两个窗口之间切换时遇到了问题。场景看起来是这样的:我打开应用程序的登陆页面,然后点击登录链接,弹出窗口打开,我输入凭证,然后点击登录按钮(弹出窗口在这里自动关闭)。之后,我必须返回原始窗口,并继续执行其他操作,因为我已经登录到应用程序中。问题是,我经常无法切换回父窗口,在弹出式窗口自动关闭,并且webdriver只是挂起,什么都不做,就不会抛出错误。
我尝试过在谷歌上找到的各种解决方案,但是没有什么是真正有效的。最后一段我尝试过的代码如下:
getLandingPageObject().performClickOnEmailLink();
getDriver().manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
Iterator<String> it = handles.iterator();
// iterate through your windows
while (it.hasNext()) {
String parent = it.next();
String newwin = it.next();
getDriver().switchTo().window(newwin);
typeLoginCredentials(username, password);
performClickOnLoginButton();
Thread.sleep(5000);
getDriver().switchTo().window(parent);
}
}
loginShouldBeSuccessful();我使用Firefox47.0.1和Selenium 2.53.1
知道我怎么解决这个问题吗?谢谢!
发布于 2016-10-03 09:02:12
发布于 2016-10-03 13:49:16
代码中使用的逻辑似乎存在问题。使用: Iterator时,它= handles.iterator();
打开的窗口数为2,因此条件循环将执行两次,而不管在其中执行的操作(即登录和自动关闭窗口)。
在第二次迭代中,父窗口句柄将被覆盖,驱动程序无法再切换到它。
为了避免这种情况,您必须在循环之外或仅在循环中一次获取和存储父窗口句柄。下面粘贴了对第一个解决方案进行修改的代码片段。搬家
String parent = it.next();在循环外部,捕获父窗口句柄并保留值。
Iterator<String> it = handles.iterator();
String parent = it.next();
// iterate through your windows
while (it.hasNext()) {
String newwin = it.next();
getDriver().switchTo().window(newwin);
typeLoginCredentials
(username,password);
performClickOnLoginButton();
Thread.sleep(5000);
}
getDriver().switchTo().window(newwin);
loginShouldBeSuccessful();https://stackoverflow.com/questions/39827521
复制相似问题