How to increase code execution time limit in Selenium webdriver using C#

ぃ、小莉子 提交于 2020-03-06 09:21:28

问题


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

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

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?

Thank you


回答1:


You can increase page load wait time beyond the 60 seconds(default), say example 70 seconds as given below.

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



回答2:


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));

Updated-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);
    }
}

Usage of above method:

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


来源:https://stackoverflow.com/questions/60386916/how-to-increase-code-execution-time-limit-in-selenium-webdriver-using-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!