9

I am running a simple piece of code that downloads a website through proxy, however sometimes the proxy can be slow and this can cause the WebDriver .get(url) request to block indefinitely.

Is there a simple piece of Python code for WebDriver that will set a timeout for this function? Through searching I've only found techniques that work for java.

driver.get(url)
1
  • How long is dies it take to download? I had situations where the page was appearing about 2 minutes later without any problems using driver.get(URL) Commented Dec 15, 2012 at 13:19

2 Answers 2

8

For all the web cretents out there what I used to solve this problem was this. Selenium uses the socket library so I set a timeout on the socket module, this throws an error which we can use to send the escape key to the browser (which stops the page loading):

socket.setdefaulttimeout(2)
try:
     driver.get(pageLink)
except socket.timeout:
     #send ESCAPE key to browser
0
4

Found this in the docs

selenium.webdriver.remote.webdriver.set_script_timeout(time_to_wait)

Set the amount of time that the script should wait before throwing an error.

time_to_wait: The amount of time to wait

Usage:

driver.set_script_timeout(30)
4
  • That looks helpful but I don't know if it works for the implicit (explicit?) wait of the .get() cal. Also WebDriver has horrible documentation so I don't know how to implement that to catch the error. Commented Dec 13, 2012 at 16:16
  • I'm trying "driver.implicitly_wait(5)" as well but it's doing nothing. I just want to end the "driver.get(url)" call if it's still running after X amount of time, but it seems the WebDriver hasn't made this easy. This link does not help either (it's always the first to come up): code.google.com/p/selenium/issues/… Commented Dec 13, 2012 at 16:25
  • Would be helpful to know what you are expecting to happen. My understanding is when the timeout happens a TimeoutException is thrown.
    – srhegde
    Commented Dec 15, 2012 at 12:10
  • @AaronHiniker case you want to force load to stop, you can send_keys to body, especially the SCAPE one. Browser will stop loading asap, but you have to ensure you look for your desired item, maybe is not loaded and Selenium will throw element not visibile exception.
    – m3nda
    Commented Jun 3, 2017 at 8:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.