4
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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Jack
  • 497
  • 3
  • 10
  • 22

1 Answers1

8

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?

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • chrome don't show reload :( , it does reload in background – Hamid Zandi Mar 24 '18 at 12:52
  • I cannot get this to work with geckodriver 0.22 and Firefox 62. I get the error 'selenium.common.exceptions.InvalidArgumentException: Message: unknown field `sessionId`, expected one of `implicit`, `pageLoad`, `script` at line 1 column 31' – skunkwerk Sep 23 '18 at 01:08
  • @skunkwerk, have you ever found out what that message was about, or how to make gecko and ff to work? – chris Dec 19 '18 at 12:50
  • @chris, nope - i ended up using puppeteer instead of selenium/webdriver. – skunkwerk Dec 28 '18 at 06:23