Jack
Jack

Reputation: 497

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?

Upvotes: 4

Views: 5621

Answers (1)

alecxe
alecxe

Reputation: 473763

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?

Upvotes: 8

Related Questions