Selenium WebDriver .getWindowHandles()返回先前关闭的窗口,如何区分这些窗口和活动窗口?

问题描述 投票:0回答:1

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

因此,当我尝试切换到窗口检查其标题时,脚本超时,因为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;
    }
java selenium automation webdriver
1个回答
0
投票

用它作为解决方法:

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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.