Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium Timeout Exception Catch

Tags:

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.

like image 257
PoweredByCoffee Avatar asked Mar 16 '16 04:03

PoweredByCoffee


People also ask

How does Python handle timeout exception in Selenium?

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.

What does timeout exception mean?

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 Webdriverexception Selenium?

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.

What is the use of pageLoadTimeout in Selenium?

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.


1 Answers

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() 
like image 116
Buaban Avatar answered Sep 22 '22 16:09

Buaban