Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium webdriver with python- how to reload page if loading takes too long?

driver = webdriver.Firefox()               #opens firefox
driver.get("https://www.google.com/")      #loads google

If it takes too long to load google, how do I make it close the browser and start the code from the beginning?

like image 431
Jack Avatar asked Feb 12 '23 07:02

Jack


1 Answers

Set page load timeout via set_page_load_timeout() and catch TimeoutException:

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

driver = webdriver.Firefox()
driver.set_page_load_timeout(10)
while True:
    try:
        driver.get("https://www.google.com/")
    except TimeoutException:
        print "Timeout, retrying..."
        continue
    else:
        break

See also: How to set Selenium Python WebDriver default timeout?

like image 165
alecxe Avatar answered Feb 15 '23 01:02

alecxe