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.
You can try using some other property to locate the element such as CSS Selector or Xpath . Use explicit waits. This will ensure all timeouts happen after the given time. This should be declared at the start of the program before carrying out any tasks.
Exception thrown when a blocking operation times out. Blocking operations for which a timeout is specified need a means to indicate that the timeout has occurred.
What is an Exception? “Exception” is a standard term used by software programmers regardless of any programming language that is used to write the code. “Exception” as the name suggests is an unusual event or an uncommon case that disrupts the normal flow of program execution.
pageLoadTimeout in Selenium This sets the time to wait for a page to load completely before throwing an error. If the timeout is negative, page loads can be indefinite.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With