【问题标题】:Selenium WebDriver .getWindowHandles() returns previously closed windows, how can I distinguish these from active windows?Selenium WebDriver .getWindowHandles() 返回以前关闭的窗口,如何将这些与活动窗口区分开来?
【发布时间】:2018-08-08 16:42:45
【问题描述】:

我正在尝试创建一个循环打开窗口并根据窗口标题切换到特定窗口的函数。这适用于前几个窗口,但我正在自动化的站点使用了几个弹出窗口,这些窗口会在“下一步”按钮等操作时自行关闭,并且这些句柄在窗口关闭后似乎继续存在。

所以,当我尝试切换到一个窗口来检查其标题时,脚本会超时,因为 WebDriver 无法切换到一个不存在的窗口。但它不会抛出 NoSuchWindowException,这很奇怪。

下面是我的遍历窗口和按标题切换的方法

public String switchByTitle(String title)
{
    boolean isPage = false;
    String neededHandle = null;

    while(isPage == false)
    {
        for (String winHandle : driver.getWindowHandles())
        {
            try 
            {
                //vvv timeout occurs here vvv
                driver.switchTo().window(winHandle);
                if (driver.switchTo().window(winHandle).getTitle().equals(title))
                {
                    isPage = true;
                    neededHandle = winHandle;
                    break;
                }
            }   
            catch (NoSuchWindowException e)
            {
                continue;
            }
        }
    }
    return neededHandle;
}

更新 不理想但我找到了解决方法

public String switchByTitle(String title)
    {
        boolean isPage = false;
        String neededHandle = null;
        WebDriverWait wait = new WebDriverWait(driver, 5);

        while(isPage == false)
        {
            for (String winHandle : driver.getWindowHandles())
            {
                try
                {   
                    //shortened timeout to 5 seconds        
                    driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
                    driver.switchTo().window(winHandle);
                    if (driver.switchTo().window(winHandle).getTitle().equals(title))
                    {
                        isPage = true;
                        neededHandle = winHandle;
                        break;
                    }
                }
                catch (NoSuchWindowException e)
                {
                    continue;
                }   
                //added timeout exception
                catch (org.openqa.selenium.TimeoutException e)
                {
                    continue;
                }
            }
        }
        //reset timeout
        driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS);
        return neededHandle;
    }

【问题讨论】:

  • 这可能不是一个不同的窗口。大多数弹出窗口都是在同一个窗口中完成的,即使它们本质上是模态的。这完全取决于 DOM,当然,大多数在现有 DOM 之外打开某些内容的 Web 应用程序都是在新选项卡中打开的,很少在新窗口(浏览器实例)中打开。
  • 在调用 getWindowHandls 方法之前尝试刷新屏幕。
  • @Bill Hileman,它不是浏览器的新实例,而是一个新窗口,具有独特的窗口句柄。我必须使用 switchTo().window(handle) 命令才能在它们之间切换,但是当旧的弹出窗口关闭时,它们的句柄仍然存在,这会导致 switchTo() 函数超时。
  • @Auro Sarma 这是我不知道的硒命令还是您的意思是刷新浏览器页面?如果是后者,我不能这样做,因为弹出窗口是模态的。
  • 请显示您的一些代码 - 在原始帖子中,而不是作为评论。

标签: java selenium automation webdriver


【解决方案1】:

将此用作解决方法:

public String switchByTitle(String title)
    {
        boolean isPage = false;
        String neededHandle = null;
        WebDriverWait wait = new WebDriverWait(driver, 5);

        while(isPage == false)
        {
            for (String winHandle : driver.getWindowHandles())
            {
                try
                {   
                    //shortened timeout to 5 seconds        
                    driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
                    driver.switchTo().window(winHandle);
                    if (driver.switchTo().window(winHandle).getTitle().equals(title))
                    {
                        isPage = true;
                        neededHandle = winHandle;
                        break;
                    }
                }
                catch (NoSuchWindowException e)
                {
                    continue;
                }   
                //added timeout exception
                catch (org.openqa.selenium.TimeoutException e)
                {
                    continue;
                }
            }
        }
        //reset timeout
        driver.manage().timeouts().pageLoadTimeout(1800, TimeUnit.SECONDS);
        return neededHandle;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2012-01-26
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    相关资源
    最近更新 更多