java Selenium Chromedriver 和 IEdriver No such Window 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32105692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:40:22  来源:igfitidea点击:

Selenium Chromedriver and IEdriver No Such Window exception

javaseleniumselenium-webdriverselenium-chromedriver

提问by Shahboz

I am having this issue in selenium where the original window is getting closed instead of a new window that is open and targeted. The code runs smoothly on firefox. But in Chrome (really fast) and IE it fails with an exception: org.openqa.selenium.NoSuchWindowException: no such window.

我在 selenium 中遇到了这个问题,其中原始窗口正在关闭,而不是打开且有针对性的新窗口。代码在 Firefox 上运行流畅。但是在 Chrome(非常快)和 IE 中它失败并出现异常:org.openqa.selenium.NoSuchWindowException:没有这样的窗口。

I think it has something to do with the speed of the test? So, chrome tries super fast when closing the window? How do you actually close the new window and switch back to the original one and interact with it?

我认为这与测试的速度有关?那么,chrome在关闭窗口时尝试超快?您实际上如何关闭新窗口并切换回原始窗口并与之交互?

Here is the snippet of my code.

这是我的代码片段。

 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

采纳答案by Andy

Can you try waiting e.g.

你可以尝试等待例如

Try adding via Explicit wait

尝试通过显式等待添加

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

回答by JeffC

Do over with new info...

用新的信息做完...

I don't know if this will work because I haven't done a lot with popups but I would start with something like this.

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

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