【问题标题】:Selenium Chromedriver and IEdriver No Such Window exceptionSelenium Chromedriver 和 IEdriver 没有这样的窗口异常
【发布时间】: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


【解决方案1】:

您可以尝试等待吗?例如

尝试通过显式等待添加

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

);

Or 
with thread sleep : 
    driver.switchTo().window(newWindow);
    System.out.println(driver.getTitle());
    Thread.sleep(3000);

【讨论】:

  • 感谢您的回答。我使用了 thread.sleep,因为其他解决方案不起作用。
【解决方案2】:

重新处理新信息...

我不知道这是否可行,因为我没有对弹出窗口做过很多事情,但我会从这样的事情开始。

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);
}

【讨论】:

  • 所以基本上,我应该说 driver.switchTo().window(newWindow).close();就我的代码而言?
  • 这对我有用......如果你不想使用完整的功能,希望对你有用。试试看...告诉我结果。
  • 还是一样的问题。我只是有一个等待的问题。该代码在 Firefox 中运行良好,只是 chrome 处理窗口太快了。
  • 编辑了我的答案以添加另一个建议。尝试等待弹出窗口存在,然后将其关闭。
  • 嘿,杰夫,我对此有点困惑。你能用我的情况向我解释一下吗?我在哪里等待,何时关闭新窗口?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
相关资源
最近更新 更多