目标窗口已从未知错误关闭:未找到Web视图

问题描述 投票:1回答:1
String mainWindow = driver.getWindowHandle();
driver.findElement(By.xpath(pro.getProperty(""))).click();

for(String newWindow : driver.getWindowHandles()){
    driver.switchTo().window(newWindow);
}

driver.close();

Log.info("Closing the current Opened Window - Invoice");
driver.switchTo().window(mainWindow); ---error in this line

切换到主窗口时出错。

org.openqa.selenium.NoSuchWindowException:没有这样的窗口:目标窗口已经因未知错误而关闭:找不到Web视图

我已经尝试切换到默认窗口,注释driver.close并添加thread.sleep但问题未解决。

java selenium selenium-webdriver
1个回答
0
投票

@ user7309686希望以下脚本能解决您的问题。最好在切换到新窗口之前进行一次验证。谢谢

public  void switchToNewWindow(WebDriver driver)
{
    String mainWindow = driver.getWindowHandle();
    Set<String> openedWindows = driver.getWindowHandles();
    if(openedWindows.size() > 1)
    {
        for(String newWindow : openedWindows)
        {
            driver.switchTo().window(newWindow);
        }
        driver.close();
        Log.info("Closing the current Opened Window - Invoice");
        driver.switchTo().window(mainWindow);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.