即使有 try...except 块也会抛出异常

Exception is thrown even though there is a try...except block

我试过了...除了块:

try:
    browser.find_element('xpath', '/html/body/ul').click()
    wait = WebDriverWait(browser, 10)
    wait.until(EC.visibility_of_element_located(('xpath', '/html/body/div[1]/div[3]/span')))
    return ' '.join(browser.find_element('id', 'response').text.split('\n')[0].split()[:-1])

except ElementNotInteractableException or NoSuchElementException or TimeoutException:
    return 'No result'

我仍然遇到这个异常:

    wait = WebDriverWait(browser, 10)
File "C:\...\selenium\webdriver\support\wait.py", line 87, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

我试过单独处理这个异常,但没有用。所以我不明白我应该如何处理它,因为代码应该忽略超时,因为它只是意味着找不到元素(这在大约 10% 的时间内是预期的)。

一个 except 子句捕获单个异常或多个异常,具体取决于关键字 except 后面的表达式的计算结果。

  1. 如果计算结果为单个异常 class,它将捕获该类型的异常。

  2. 如果计算结果为元组,它将捕获元组中包含的任何类型的异常。

表达式 ElementNotInteractableException or NoSuchElementException or TimeoutException 的计算结果为单一类型 ElementNotInteractableException,而不是包含三个异常 classes.

的元组

你想要一个明确的元组:

except (ElementNotInteractableException, NoSuchElementException, TimeoutException):
    ...