Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the timeout of 'driver.get' for python selenium 3.8.0?

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?

like image 717
Alex Avatar asked Dec 07 '17 12:12

Alex


People also ask

How do I change the default timeout in selenium?

If you want to ensure that an error is thrown if your page takes longer than expected to load, you can modify this by using this line of code: driver. manage(). timeouts().

How does Python handle timeout exception in selenium?

You can try using some other property to locate the element such as CSS Selector or Xpath . Use explicit waits. This will ensure all timeouts happen after the given time. This should be declared at the start of the program before carrying out any tasks.

What is script timeout in selenium?

scriptTimeout. default WebDriver.Timeouts scriptTimeout​(java.time.Duration duration) Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, not null, or greater than 2e16 - 1, an error code with invalid argument will be returned.


1 Answers

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)
like image 59
undetected Selenium Avatar answered Oct 19 '22 04:10

undetected Selenium