【问题标题】:How to add different except expectations to one [duplicate]如何将不同的期望添加到一个[重复]
【发布时间】:2021-08-09 02:51:59
【问题描述】:

我正在使用 Python Selenium Chromedriver,但遇到了一个问题。因此,我想在一行中添加除期望之外的其他内容,例如将 TimeoutExpectations 和 ElementClickInterceptedException 添加到一行。 我当前的代码:

        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "test2"))).click()
        WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "test2")))
    except TimeoutException:
                for _ in range(1000):
                    print("Not available... trying again...")
                    driver.refresh()
                    ATC()

我希望这部分看起来像:

except TimeoutException, ElementClickInterceptedException:

如何让它发挥作用?

【问题讨论】:

    标签: python exception


    【解决方案1】:

    您可以在一行中捕获多个异常,如下所示:

    try:
        your_code_that_may_rise_exceptions
    except (SpecificErrorOne, SpecificErrorTwo) as error:
        handle(error)
    

    或略有不同的语法:

    try:
        your_code_that_may_rise_exceptions
    except (SpecificErrorOne, SpecificErrorTwo), e:
        handle(e)
    

    【讨论】:

      【解决方案2】:

      这可以通过插入以下except 语句来实现:

      except (TimeoutException, ElementClickInterceptedException):
      

      【讨论】:

        猜你喜欢
        • 2012-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-16
        • 2013-07-14
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多