【问题标题】:How to force Selenium to continue to next process without waiting to complete loading a page如何强制 Selenium 继续下一个进程而不等待完成加载页面
【发布时间】:2016-12-25 17:00:52
【问题描述】:

通常,在导航到一个 url 后,Selenium WebDriver 等待完成页面以继续下一个进程(下一行代码)。如果该过程需要很长时间,那么它将抛出超时异常。我不想等太久。导航到一个 url 10 秒后,我不想等到它完成。

        using (IWebDriver driver = new FirefoxDriver())
        {
            driver.Navigate().GoToUrl("www.mywebsite.com"); //takes a long time here. More than 40 seconds to complete whole page
            //here I don't want to wait too long
            // <input type="text" id="tp-test-selenium" />
            var element = driver.FindElement(By.Id("tp-test-selenium"));  //I should be able to access this text input without waiting 40 seconds
        }

即使页面没有完全加载,我也想继续获取元素。

【问题讨论】:

  • 如果没有完全加载页面,你怎么能得到元素???这有意义吗??
  • @SaurabhGaur 是的。用ajax加载图片。我已经在 Selenium 显示的浏览器中进行了测试
  • 我清楚,你不等待用ajax加载图像或加载整页??
  • @SaurabhGaur 他们俩。如果您的浏览器加载一个页面,它有时不会完全加载,尤其是由于互联网连接不佳。未加载 css、javascript 文件或其他文件。大多数情况下,大多数元素都已加载。

标签: c# selenium selenium-webdriver


【解决方案1】:

您可能希望将页面加载超时设置为所需的秒数。

driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(10));

然后可能用 try/catch 包装 GotoUrl()

using (IWebDriver driver = new FirefoxDriver())
{
    try
    {
        driver.Navigate().GoToUrl("www.mywebsite.com"); //takes a long time here. More than 40 seconds to complete whole page
    }
    catch(WebPageTimeoutException e)
    {
        //Log it or not
    }
    finally
    {
        // It will perform your steps either if page loaded correctly during timeout set or after timeout expired
        //here I don't want to wait too long
        // <input type="text" id="tp-test-selenium" />
        var element = driver.FindElement(By.Id("tp-test-selenium"));  //I should be able to access this text input without waiting 40 seconds
    }
}

【讨论】:

  • 谢谢,我试试看。
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 2018-05-28
  • 2011-07-08
  • 2023-01-28
  • 1970-01-01
  • 2018-03-20
相关资源
最近更新 更多