【问题标题】:Is there a way to throw all Selenium exceptions in Python as a single parent exception type?有没有办法将 Python 中的所有 Selenium 异常作为单个父异常类型抛出?
【发布时间】:2022-11-07 22:15:28
【问题描述】:

我正在开发一套 Selenium 刮板,并在一个地方监控刮板运行的结果。能够快速查看日志并查看刮板错误是否是我预期和引发的错误与 Selenium 引发的意外错误会很有帮助。 Selenium 可以在运行时抛出各种不同的错误,例如 NoSuchElementException、InvalidElementStateException 等。

我知道这些异常可以从 selenium.common.exceptions 导入和有目的地引发。为了我的日志记录,有没有办法将整个异常类滚动到一个类中?我可以以某种方式定义一个父类 SeleniumException 并将所有 Selenium 异常映射到这个吗?这样,在我的日志中就会显示发生了 SeleniumException。

如果有人有想法,我将不胜感激。我正在使用 Python 3。

【问题讨论】:

    标签: python selenium exception logging kibana


    【解决方案1】:

    它们都继承自selenium.common.exceptions.WebDriverException(),因此您可以使用该类捕获它们。从那里你可以为他们创建一些日志。例如像这样:

    try:
        # code that can throw selenium exceptn
    except selenium.common.exceptions.WebDriverException as e:
        logging.exception('a WebDriverException happened')
    

    【讨论】:

      猜你喜欢
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      相关资源
      最近更新 更多