[英]Selenium work around 'TimeoutException' is not defined?
我想解决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
在我们遇到2.2)
中的异常之后,我永远无法到达第3.)
步。)并且 try/catch 不起作用。
在第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
我无法从您的示例中看到您的导入语句,因此请确保您有
从.py
文件顶部的from selenium.common.exceptions import TimeoutException
。
从 selenium 异常中导入TimeoutException
,如下所示:
from selenium.common.exceptions import TimeoutException
将 Function 放入 try 块本身:
try:
getByClass = WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.CLASS_NAME, InputElement)))
element = Dom.getByClass('test-class', driver).text
except TimeoutException:
element = 'element not found'
print(element)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.