我在selenium中遇到了这个问题,原来的窗口正在关闭,而不是一个打开和定位的新窗口。代码在firefox上运行顺畅。但在Chrome(真的很快)和IE它失败,但有一个例外:org.openqa.selenium.NoSuchWindowException:没有这样的窗口。
我认为它与测试的速度有关?那么,关闭窗口时,chrome尝试超快?你如何实际关闭新窗口并切换回原来的窗口并与之交互?
以下是我的代码片段。
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
答案 0 :(得分:0)
完成新信息...
我不知道这是否会奏效,因为我还没有完成弹出窗口,但我会从这样的事情开始。
public static void main(String[] args)
{
// navigate to the page that spawns popups
while (driver.getWindowHandles().size() == 1)
{
// we're waiting for a popup to be spawned
}
closePopups(driver);
}
private static void closePopups(WebDriver driver)
{
// closes all browser windows other than the main window, e.g. popups
// NOTE: it will not close other instances of the same browser, only those spawned by the current driver
String currentHandle = driver.getWindowHandle();
for (String handle : driver.getWindowHandles())
{
if (!handle.equals(currentHandle))
{
driver.switchTo().window(handle).close();
}
}
driver.switchTo().window(currentHandle);
}
答案 1 :(得分:0)
你可以尝试等待吗。
尝试通过显式等待添加
CloudBlobDirectory.ListBlobs
);
void checkAlert() {
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
} catch (Exception e) {
//exception handling
}
}
Or
implicit wait
driver.switchTo().window(newWindow);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS