首页 > 解决方案 > 未定义围绕“TimeoutException”的 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

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

步骤之后3.)还有许多其他步骤。我怎样才能让程序围绕这个超时流动。当元素不存在时,它会超时。

代码

    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)

结果

    except TimeoutException:
    NameError: name 'TimeoutException' is not defined

标签: pythonselenium

解决方案


我无法从您的示例中看到您的导入语句,因此请确保您有

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


推荐阅读