75

Trying to find a good way to set a maximum time limit for command execution latency in Selenium Python WebDriver. Ideally, something like:

my_driver = get_my_driver()
my_driver.set_timeout(30) # seconds
my_driver.get('http://www.example.com') # stops / throws exception when time is over 30     seconds

would work. I have found .implicitly_wait(30), but I'm not sure if it results in the desired behavior.

In case it is useful, we are specifically using the WebDriver for Firefox.

EDIT

As per @amey's answer, this might be useful:

ff = webdriver.Firefox()
ff.implicitly_wait(10) # seconds
ff.get("http://somedomain/url_that_delays_loading")
myDynamicElement = ff.find_element_by_id("myDynamicElement")

However, it is not clear to me whether the implicit wait applies both to get (which is the desired functionality) and to find_element_by_id.

Thanks very much!

2
  • 1
    I had a look at the source code. It's vague for python binding. But for C#, ImplicitlyWait only works for FindElement/FindElements (same for Java). Source: 1 2
    – Yi Zeng
    Jul 8, 2013 at 21:08
  • Thanks. See my answer below if you're interested. Jul 8, 2013 at 21:45

4 Answers 4

137

In python, the method to create a timeout for a page to load is:

Firefox, Chromedriver and undetected_chromedriver:

driver.set_page_load_timeout(30)

Other:

driver.implicitly_wait(30)

This will throw a TimeoutException whenever the page load takes more than 30 seconds.

2
  • set_page_load_timeout works in chromedriver 75 for me
    – Stoopkid
    Aug 30, 2019 at 16:17
  • This fails with Python 3.7.4, selenium 3.141.0, and gecko 0.26.0. Nov 3, 2019 at 18:57
9

The best way is to set preference:

fp = webdriver.FirefoxProfile()
fp.set_preference("http.response.timeout", 5)
fp.set_preference("dom.max_script_run_time", 5)
driver = webdriver.Firefox(firefox_profile=fp)

driver.get("http://www.google.com/")
3
  • @ivan_bilan: If you mean an Exeption, no, it doesn't return any Nov 27, 2015 at 16:23
  • dom.max_script_run_time sets a timeout for executing javascript. It's not a full pageload timeout. Dec 1, 2015 at 22:26
  • This fails with Python 3.7.4, selenium 3.141.0, and gecko 0.26.0. Nov 3, 2019 at 18:57
7

Information about Explicit and Implicit waits can be found here.

UPDATE

In java I see this, based of this :

WebDriver.Timeouts pageLoadTimeout(long time,
                                 java.util.concurrent.TimeUnit unit)

Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

Parameters:
    time - The timeout value.
    unit - The unit of time.

Not sure of the python equivalent.

0
5

My solution was to run an asynchronous thread alongside the browser load event, and have it close the browser and re-call the load function if there was a timeout.

#Thread
def f():
    loadStatus = true
    print "f started"
    time.sleep(90)
    print "f finished"
    if loadStatus is true:
        print "timeout"
        browser.close()
        call()

#Function to load
def call():
    try:
        threading.Thread(target=f).start()
        browser.get("http://website.com")
        browser.delete_all_cookies()
        loadStatus = false
    except:
        print "Connection Error"
        browser.close()
        call()

Call() is a function which just

2
  • 1
    Your recursion is missing a break condition in case the website is offline. If it's intended, use an infinite loop with a break on success.
    – Git.Coach
    May 20, 2021 at 19:36
  • Nice universal solution without touching selenium stuff. Aug 11, 2021 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.