Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium work around 'TimeoutException' is not defined?

I want to work around the TimeOutException in Python Selenium.

This happens:

1.) Open Browser, Call URL
2.) Trying to find Element
2.1) Works sometimes
2.2) Works not sometimes and throws TimeoutException
3.) Should look for other elements

I can never reach step 3.) after we have hit the exception in 2.2) and try/catch does not work.

After step 3.) there are many other steps. How can I let the program flow around this timeout. It is timeouting when the element is not there.

Code

    def getByClass(InputElement, driver):
        getByClass = WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.CLASS_NAME, InputElement)))
        return getByClass

    try:
        element = Dom.getByClass('test-class', driver).text
    except TimeoutException:
        element = 'element not found'
    print(element)

Result

    except TimeoutException:
    NameError: name 'TimeoutException' is not defined
like image 892
stevek-pro Avatar asked May 18 '26 07:05

stevek-pro


1 Answers

I can't see your import statements from your sample, so make sure you have

from selenium.common.exceptions import TimeoutException at the top of your .py file.

like image 167
CEH Avatar answered May 19 '26 22:05

CEH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!