Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Selenium webdriver after X seconds

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.

like image 629
FlyingZebra1 Avatar asked Oct 15 '25 16:10

FlyingZebra1


2 Answers

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.

like image 177
n1c9 Avatar answered Oct 17 '25 06:10

n1c9


When the page isn't loading and and the whole script simply hangs the solution would be to configure set_page_load_timeout().

Here is an effective code block catching the TimeoutException. Irrespective of TimeoutException happening or not remember to call quit() method within try-except{} block to keep away the dangling instances of the WebDriver variants.

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:
    print("Page load Timeout Occured. Quiting !!!")
    driver.quit()
like image 27
undetected Selenium Avatar answered Oct 17 '25 06:10

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!