python - 未定义围绕“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
解决方案
我无法从您的示例中看到您的导入语句,因此请确保您有
from selenium.common.exceptions import TimeoutException
在.py
文件的顶部。
推荐阅读
- flutter - 当我关闭我的应用程序时,我的数据库正在重置
- javascript - 动态地将类添加到 React map 函数中的前 n 个元素
- mysql - 我需要通过以下方式加入这两个表..使用mysql
- prismjs - 如何在 Scully 中突出显示代码块?
- axon - Axon Saga 订阅模式并发问题
- kubernetes - 嗨,我正在尝试让客户端 real-ip 限制 kubernetes 环境上 pod 的某些访问
- docker - 如何从运行在同一 Kubernetes 集群中的容器连接到在 Kubernetes 集群中的主机 VM 上运行的 Docker?
- python - Micropython构建外部C模块
- android - 如何观察更改对象 Kotlin
- java - 如何从用户输入中获取随机字符并将随机字符存储到数组中?