Selenium 围绕“TimeoutException”的工作未定义?

mes*_*lds 3 python selenium

我想解决Python Selenium 中的TimeOutException

有时候是这样的:

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
Run Code Online (Sandbox Code Playgroud)

3.)在我们遇到异常2.2)并且 try/catch 不起作用之后,我永远无法到达步骤。

步骤之后3.)还有许多其他步骤。我怎样才能让程序在这个超时时间内运行。当元素不存在时,就会超时。

代码

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
Run Code Online (Sandbox Code Playgroud)

结果

    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)
Run Code Online (Sandbox Code Playgroud)

Chr*_*ine 15

我看不到你的样本中的导入语句,所以请确保你有

from selenium.common.exceptions import TimeoutException 在你的文件的顶部.py