Jack Jack - 4 months ago 49
Python Question

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?

Answer

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?

Comments