10

In selenium tests, you open a webpage using

from selenium import webdriver
driver = webdriver.Firefox()
driver.get("whateverpage.org.com")

How can I set the timeout of this command for selenium version 3.8.0 and python 2.7.12?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Alex
  • 41,580
  • 88
  • 260
  • 469
  • Do you need to set the page timeout to quickly fail a test or do you need it to interact with a page that is not yet complete? – Florent B. Dec 07 '17 at 13:52
  • I want to increase the timeout as the jenkins test-instance running the tests is EXTREMLY slow and I suspect the test fails because of a too small timeout... – Alex Dec 07 '17 at 14:02
  • 1
    Rather than trying to increase the timeout, you should try to figure out why this is appending and fix it. It could be due to a resource in the page which is no reachable. You also have to consider the timeout of the connection which will raise an exception first if it is lower than the timeout for the page: `driver.command_executor.set_timeout()`. – Florent B. Dec 07 '17 at 14:13
  • But its good to know that there is YET another timeout to consider. But how to change the page-load timeout in the first place? Do you know how to do that or not? – Alex Dec 07 '17 at 14:21
  • 1
    @DebanjanB already answered your question: `driver.set_page_load_timeout`. If it doesn't work, then it's a bug which should be reported to https://github.com/mozilla/geckodriver/issues – Florent B. Dec 07 '17 at 14:29
  • Yes maybe its a bug which I was not aware of. I am trying to use geckodriver 0.18.0 instead of 0.16.0. But I learned this information in the last 2 minutes, so please forgive my ignorance on all these extremely complex things... – Alex Dec 07 '17 at 14:34

1 Answers1

14

To set the time out for Page Loading you can induce the set_page_load_timeout(seconds).


set_page_load_timeout


Method Details

def set_page_load_timeout(self, time_to_wait):
    """
    Set the amount of time to wait for a page load to complete
    before throwing an error.

Args

time_to_wait: The amount of time to wait

Usage

driver.set_page_load_timeout(3)

Example

from selenium import webdriver
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.set_page_load_timeout(2)
try :
    driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl")
    print("URL successfully Accessed")
    driver.quit()
except TimeoutException as e:
    print("Page load Timeout Occurred. Quitting !!!")
    driver.quit()

Console Output

Page load Timeout Occurred. Quitting !!!

Documentation

You can find a detailed discussion on pageLoadTimeout here pageLoadTimeout in Selenium not working


Deep Dive

As per Python 3.x if we don't handle the exception the following log messages are observed :

    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
  (Session info: chrome=62.0.3202.94)
  (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.2.9200 x86_64)
Bobort
  • 3,085
  • 32
  • 43
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • error: `... self.driver.set_page_load_timeout(30) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 833, in set_page_load_timeout 'type': 'page load'}) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute self.error_handler.check_response(response) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response raise exception_class(message, screen, stacktrace) WebDriverException: Message: timeouts` – Alex Dec 07 '17 at 12:45
  • I got an error because I used geckodriver 0.16.0 instead of 0.18.0. That is why I got the error. The documentation page `http://selenium-python.readthedocs.io/api.html` is completly misleading and incomplete! – Alex Dec 08 '17 at 14:51