本文介绍了如何使用C#增加Selenium Webdriver中的代码执行时间限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网站从网站请求数据时需要花费时间.

Website is taking time to load when requesting a data from it.

如果网站在60秒内加载,则一切正常.超过那个时间,它使我进入错误部分.

If website loads within 60 seconds, then everything is ok. Beyond that time, it throws me to error section.

实际上,此代码仅执行60个部分.

Actually, this code is executing for 60 sections only.

this.driver.FindElement(By.CssSelector("#btnLogin")).Click();

如何设置驱动程序以等待该代码完全执行?

How to set the driver to wait for this code to be executed completely?

谢谢

推荐答案

有很多选项可以设置页面加载超时.

There is many option to set page load time out.

driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(90);

OR

driver.Manage().Timeouts().ImplicitWait.Add(System.TimeSpan.FromSeconds(90)); 

OR

driver.Manage().Timeouts().PageLoad.Add(System.TimeSpan.FromSeconds(90));

OR

driver.Manage().Timeouts().AsynchronousJavaScript.Add(90));

已更新-1

您可以尝试在新页面上等待元素的出现(页面加载后会显示页面).编写一个自定义方法来查找具有超时的元素,如下所示:

You can try to wait an element’s presence on new page (page appears after page load). Write a custom method to find element with timeout like below

public static class WebDriverExtensions
{
    public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
    {
        if (timeoutInSeconds > 0)
        {
            var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
            return wait.Until(driver => driver.FindElement(by));
        }
        return driver.FindElement(by);
    }
}

上述方法的用途:

driver.FindElement(By.CssSelector("#btnLogin"), 90);

这篇关于如何使用C#增加Selenium Webdriver中的代码执行时间限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:27