Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Selenium 2.20 WebDriver to create and manage a firefox browser with C#. To visit a page, i use the following code, setting the driver timeouts before visiting the URL:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); // Set implicit wait timeouts to 5 secs driver.Manage().Timeouts().SetScriptTimeout(new TimeSpan(0, 0, 0, 5));  // Set script timeouts to 5 secs driver.Navigate().GoToUrl(myUrl);   // Goto page url 

The problem is that sometimes pages take forever to load, and it appears that the default timeout for a page to load using the selenium WebDriver is 30 seconds, which is too long. And i don't believe the timeouts i am setting apply to the loading of a page using the GoToUrl() method.

So I am trying to figure out how to set a timeout for a page to load, however, i cannot find any property or method that actually works. The default 30 second timeout also seems to apply to when i click an element.

Is there a way to set the page load timeout to a specific value so that when i call the GoToUrl() method it will only wait my specified time before continuing?

like image 673
KabanaSoft Avatar asked May 15 '12 18:05

KabanaSoft


People also ask

How do I set Selenium timeout?

The setScriptTimeout is the method to set the time for the webdriver. This is usually applied for an asynchronous test to complete prior throwing an exception. The default value of timeout is 0. This method is generally used for JavaScript commands in Selenium.

What is page load timeout and how you set it?

The pageLoadTimeout is the method used to set the time for the entire page load prior to throwing an exception. If the timeout time is set to negative, then the time taken to load the page is endless. This timeout is generally used with the navigate and manage methods.

What is driver manage () timeouts () implicitlyWait?

1. implicitlyWait() This timeout is used to specify the amount of time the driver should wait while searching for an element if it is not immediately present.

What is the default page load timeout in Selenium WebDriver?

This defines the amount of time that Selenium will wait for a page to load. By default, it is set to 0 (which equates to an infinite time out).


1 Answers

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

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

like image 105
Josh Avatar answered Sep 20 '22 13:09

Josh