37

In my threads I use a simple variable either set to '1' or '0' to indicate if it is ready to go again. Trying to debug an issue where sometimes this isn't being reset and I think I might have it.

I didn't want connections timing out into some infinite load time (I believe the default for Selenium is not to have a timeout) so I used:

Driver.set_page_load_timeout(30)

And later on in that thread I would check

If condition:
 isrunning = 0

I had originally thought that the set_page_load_timeout would just stop it loading after 30 seconds but if I'm understanding this correctly it would actually throw an exception so I'd need to do something like:

try:
  Driver.set_page_load_timeout(30)
except:
  isrunning = 0
  Driver.Close()

-Do whatever else in function -
If condition:
  isrunning = 0
  Driver.Close()

So if it ran over 30 seconds it would close and set to 0 otherwise it would run on and get checked and set to 0 later.

I appreciate this is a tiny snippet of code but the full thing is pretty long winded and I think that's the important part.

I'd appreciate it if someone could confirm I have the right idea here. I'm all up for doing the testing but it's a problem which occurs once every 8 hours or so making it hard to pick apart but I think this potentially fits.

1
  • a lot of text, but what is a question?) How to intercept TimeOutException or what? Commented Mar 16, 2016 at 5:16

2 Answers 2

79

Almost of your code is working fine except the Driver.Close(). It should be Driver.close(). The TimeoutException will be thrown when the page is not loaded within specific time. See my code below:

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

Driver = webdriver.Firefox()
try:
    Driver.set_page_load_timeout(1)
    Driver.get("http://www.engadget.com")
except TimeoutException as ex:
    isrunning = 0
    print("Exception has been thrown. " + str(ex))
    Driver.close()
Sign up to request clarification or add additional context in comments.

3 Comments

In my code there is except TimeoutException:. However, in case of selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from renderer: xxx the exception is not caught leading to printing Traceback (most recent call last):, Stacktrace:, and Backtrace: followed by abnormal exit of the Python script. Why? How to correctly catch this exception?
@pmor I think your error is outside of try/catch. Please check the line number that throws the error.
Thanks! I thought that Selenium provides access to a partial DOM (hence, I do page_source even in case of TimeoutException), however, it doesn't.
1

this should work

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

try:
    Driver.set_page_load_timeout(1)
    Driver.get("http://www.engadget.com")
except TimeoutException
    pass
isrunning = 0
Driver.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.