0

I'm using Python Selenium with Chromedriver. Every once in a while, a webdriver.get() call will throw a TimeoutException. I'm successfully catching every other exception through explicit waits, but the TimeoutException seems to occur when the network stream gets dropped.

What I want to do is modify the webdriver.get() method (through overrides or subclassing) so that every time my application calls get(), it will automatically:

  • Catch and handle TimeoutException
  • Retry the get() request a few times

How do I accomplish this?

Note: This question is not a duplicate of How to set the timeout of 'driver.get' for python selenium 3.8.0? -- I'm trying to add implicit functionality to the get() method. The reason I don't just wrap my get() calls in a try/except block manually is because I'm making a lot of them througout my application and am attempting to be DRY.

gnome
  • 53
  • 4

2 Answers2

0

I think that the best way is to do a try/except and make the except only catch the TimeOutException

from selenium.common.exceptions import TimeoutException
try:
    webdriver.get(url)
except TimeoutException:
    time.sleep(5)
    webdriver.get(url)

If the problem is that you have to do this several times encapsulate it in a method

0

I figured it out. You have to subclass EventFiringWebDriver like this:

from selenium.support.events import EventFiringWebDriver

class MyWebDriver(EventFiringWebDriver):
    def get( self ,url):
        try:
            super().get(url)
        except TimeoutException:
            # your exception handling code goes here
gnome
  • 53
  • 4