【问题标题】:Selenium WebDriverWait does not throw timeout exceptionSelenium WebDriverWait 不抛出超时异常
【发布时间】:2019-10-13 00:58:23
【问题描述】:

我正在使用 selenium 来抓取网页。网络类似于谷歌地图,我需要输入一个开始位置和结束位置,然后得到结果。网页加载缓慢,有时会停止响应。所以我尝试使用 WebDriverWait 来捕获超时异常并重新启动网页。

然而,事实上,如果网页停止响应,webDriverWait 不会抛出超时异常,代码只会永远卡住。 比如上次,我的代码卡在 invisibility_of_element_located 并且即使超时设置为 10s 也没有响应。

WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.XPATH, "//*[@ng-show='route.isCalculating']")))

【问题讨论】:

  • invisibility_of_element_located 检查元素在 DOM 上是否不可见或不存在的期望可能是条件为真,这就是它不抛出超时异常的原因。由于我对您的代码一无所知,因此我只假设它可能会发生。

标签: python selenium webdriverwait


【解决方案1】:

您可以尝试将等待包装在 try / except 块中,以查看是否可以捕获TimeOutException

from selenium.common.exceptions import TimeoutException

try:
    print("Attempting to locate element")
    WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.XPATH, "//*[@ng-show='route.isCalculating']")))
except TimeoutException:
    print("TimeoutException encountered")
print("Task complete")

根据控制台中打印的内容,您可能会确定是否遇到了异常。

如果TimeoutException 没有被捕获,您可以使用except: 看看是否有任何东西被捕获。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-16
    • 2020-10-06
    • 2013-02-22
    • 2018-07-14
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多