Retry for Selenium Exception in Subproces for Python

Submitted 4 years, 7 months ago
Ticket #129
Views 878
Language/Framework Python
Priority Medium
Status Closed

0

I have a python with selenium script that should be called with an argument that different for each person (I called it Tester.py), to make it easier I put it in a new python file and run it with a #subprocess like this :

# Explanation about retry is next
@retry(retry_count=5, delay=5)

def job():    
    try:
        subprocess.Popen("python Tester.py -user Margareth", shell=True)
        return True
    except subprocess.CalledProcessError:
        return False
        print("System Retrying")
        pass

my retry wrapper are look like this (but It's not working) :

def retry(retry_count=5, delay=5, allowed_exceptions=()):
    def decorator(f):
        @functools.wraps(f)
        def wrapper(*args, **kwargs):
            for _ in range(retry_count):
                try:
                    result = f(*args, **kwargs)
                    if result:
                        pass
                except allowed_exceptions as e:
                    pass
                time.sleep(delay)
        return wrapper
    return decorator

My only problem with Tester.py is TimeoutException from selenium, and I want to retry it if it failed for x times and x seconds delay, but somehow #try-catch not working and my retry always result in random second retry and it really messed, Any clue?

Submitted on Sep 10, 20
add a comment

1 Answer

Verified

from retry import retry
from timeout_decorator import timeout, TimeoutError
from selenium import webdriver
from selenium.common.exceptions import TimeoutException    

@retry(TimeoutError, tries=3)
@timeout(10)
def get_with_retry(driver, url):
    driver.get(url)


def main():
    url = "http://something.foo"

    driver=webdriver.Firefox() 
    try:
        get_with_retry(driver, url)
        foo(driver) # do whatever it is you need to do
    finally:
        driver.quit()


if __name__ == "__main__":
    main()

Submitted 4 years, 6 months ago


Latest Blogs