3

I am using selenium webdriver (firefox) on Ubuntu + Python, and run into an issue where sometimes the page doesn't load, and the whole script simply hangs.
Is there a way to force-exit the webdriver window after X seconds ? Looking for code like the one below, that actually works though. It looks like if the webdriver is waiting on the response, it will wait (almost) indefinitely).

driver.get(record)
sleep(5)
my_html = driver.page_source #get whatever we have after 5 sec
driver.close() #close driver

NOTE!: The accepted answer is correct. The issue was caused by my geckodriver being out of date (v 0.11 vs v.019).
To check your version on ubuntu:

geckodriver --version   #command in terminal

to update the driver (if needed), use these steps. Note - Sandeep's answer worked best for me.

FlyingZebra1
  • 1,157
  • 15
  • 23
  • Possible duplicate of [pageLoadTimeout in Selenium not working](https://stackoverflow.com/questions/45591282/pageloadtimeout-in-selenium-not-working) – undetected Selenium Jan 23 '18 at 05:17

2 Answers2

2
from selenium import webdriver
from time import sleep

record = "https://www.google.com"
driver = webdriver.Firefox()
driver.set_page_load_timeout(30)
try:
    driver.get(record)
    my_html = driver.page_source #get whatever we have after 5 sec
finally:
    driver.close()

Setting page load timeout, as described here, will achieve what you're after. It will raise a TimeoutException if the page doesn't load within the time given, closing the program.

n1c9
  • 2,572
  • 3
  • 29
  • 49
  • 1
    thanks, any thought on why running the timeout code on ubuntu + python 2.7 would throw an error on the timeout line? getting the following message: "WebDriverException: Message: Failed to decode response from marionette" – FlyingZebra1 Jan 23 '18 at 05:35
  • 1
    this was the issue with outdated geckodriver on my machine. oops :( – FlyingZebra1 Jan 23 '18 at 07:19
0

When the page isn't loading and and the whole script simply hangs the solution would be to induce set_page_load_timeout. Here is effective code block and the resulted TimeoutException. Irrespective of TimeoutException happening or not remember to call quit() method within try{} and catch{} blocks to keep away the dangling instances of the webdriver variants.

from selenium import webdriver

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 :
    print("Page load Timeout Occured. Quiting !!!")
    driver.quit()
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
  • thanks, any thought on why running the timeout code on ubuntu + python 2.7 would throw an error on the timeout line? getting the following message: "WebDriverException: Message: Failed to decode response from marionette" – FlyingZebra1 Jan 23 '18 at 05:35
  • Message seems to be clear, `webdriver` unable to negotiate terms with `marionette`. You may require to clean up your system. If my **`Answer`** have catered to your **`Question`** please **`Accept`** the **`Answer`** by clicking on the tick mark beside my **`Answer`** just below the **`VoteDown`** arrow so the tick mark turns green. – undetected Selenium Jan 23 '18 at 05:38