【发布时间】:2015-08-22 08:55:25
【问题描述】:
我在 selenium 中遇到了这个问题,原来的窗口正在关闭,而不是一个打开并有针对性的新窗口。代码在firefox上运行流畅。但在 Chrome(非常快)和 IE 中,它失败并出现异常:org.openqa.selenium.NoSuchWindowException:没有这样的窗口。
我认为这与测试的速度有关?那么,chrome 在关闭窗口时会尝试超快吗?你如何真正关闭新窗口并切换回原来的窗口并与之交互?
这是我的代码的 sn-p。
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String originalWindow = driver.getWindowHandle();
String newWindow;
Set<String> windowHandles = driver.getWindowHandles();
Iterator<String> stringIterator = windowHandles.iterator();
while (stringIterator.hasNext()) {
newWindow = stringIterator.next();
if (!originalWindow.equalsIgnoreCase(newWindow)) {
driver.switchTo().window(newWindow);
System.out.println("The title of the page is: “ + driverInstance.getTitle());
}
}
driver.close(); ///In here I should close the new window
driver.switchTo().window(originalWindow); ///In here I should switch back to the old window
【问题讨论】:
-
所以,chrome 在 driver.close(); 之后就失败了;因为在那之后我切换到原始窗口并与之交互。
标签: java selenium selenium-webdriver selenium-chromedriver