Selenium WebDriver - How to set Page Load Timeout using C#

[亡魂溺海] 提交于 2019-11-28 04:30:37
ktam33

In case this helps anyone still looking for the answer to this, the C# WebDriver API now contains the appropriate method.

driver.Manage().Timeouts().SetPageLoadTimeout(timespan)
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(5);

Note: driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5)) is now deprecated.

With this you should be able to declare a wait explicitly.

WebDriverWait wait = new WebDriverWait(browser, new TimeSpan(time in seconds));
wait.until(Your condition)

you could also change the implicit wait time

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

I think that is the syntax in C#. (not to sure)

In ruby it is

@driver.manage.timeouts.implicit_wait = 30
@wait = Selenium::WebDriver::Wait.new(:timeout => 30)

i found the solution this this issue. When creating a new FirefoxDriver, there are overloads in the constructor that allow you to specify a command timeout which is the maximum time to wait for each command, and it seems to be working when calling the GoToUrl() method:

driver = new FirefoxDriver(new FirefoxBinary(), profile, new TimeSpan(0, 0, 0, timeoutSeconds));

link to FirefoxDriver constructor documentation for reference: http://selenium.googlecode.com/svn/trunk/docs/api/dotnet/html/M_OpenQA_Selenium_Firefox_FirefoxDriver__ctor_2.htm

Hope this helps someone else who runs into this problem.

As of 2018: Besides these:

driver.Manage().Timeouts().ImplicitWait.Add(System.TimeSpan.FromSeconds(5));      
driver.Manage().Timeouts().PageLoad.Add(System.TimeSpan.FromSeconds(5));
driver.Manage().Timeouts().AsynchronousJavaScript.Add(timespan));

wait for searching for an item, loading a page, and waiting for script respectively. There is:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

We brazilians have a word for crappy workarounds "Gambiarra"... Well... at least they do the job... Here is mine:

var url = "www.your.url.here"
try {
    DRIVER.Navigate().GoToUrl(url);
} catch {
    // Here you can freely use the Selenium's By class:
    WaitElement(By.Id("element_id_or_class_or_whatever_to_be_waited"), 60);
}
// rest of your application

What my WaitElement(By, int) does:

/// <summary>
/// Waits until an element of the type <paramref name="element"/> to show in the screen.
/// </summary>
/// <param name="element">Element to be waited for.</param>
/// <param name="timeout">How long (in seconds) it should be waited for.</param>
/// <returns>
/// False: Never found the element. 
/// True: Element found.
/// </returns>
private bool WaitElement(By element, int timeout)
{
    try {
        Console.WriteLine($" - Waiting for the element {element.ToString()}");
        int timesToWait = timeout * 4; // Times to wait for 1/4 of a second.
        int waitedTimes = 0; // Times waited.
        // This setup timesout at 7 seconds. you can change the code to pass the 

        do {
            waitedTimes++;
            if (waitedTimes >= timesToWait) {
                Console.WriteLine($" -- Element not found within (" +
                $"{(timesToWait * 0.25)} seconds). Canceling section...");
                return false;
            }
            Thread.Sleep(250);
        } while (!ExistsElement(element));

        Console.WriteLine($" -- Element found. Continuing...");
    //  Thread.Sleep(1000); // may apply here
        return true;
    } catch { throw; }
}

After this, you can play with timeout...

Make By the things you notice that loads last in the page (like javascript elements and captchas) remembering: it will start working the // rest of your application before the page fully loads, therefore may be nice to put a Thread.Sleep(1000) at the end just to be sure...

Also notice that this method will be called AFTER the 60 seconds standard timeout from Selenium's DRIVER.Navigate().GoToUrl(url);

Not the best, but... as I said: A good gambiarra gets the job done...

Page load timeouts are not implemented in the .NET bindings yet. Hopefully they will be soon.

For anyone who wants the opposite effect: setting timeout longer than 60s.

You need both use:

new FirefoxDriver(FirefoxDriverService.CreateDefaultService(), new FirefoxOptions(), TimeSpan.FromSeconds(120))

and

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

The new FirefoxDriver(binary, profile, timeSpan) has been obsolete.

driver.Manage().Timeouts().SetPageLoadTimeout(timespan) 

does not work.

This works. Use a property setter syntax.

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