-2

I have got this 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'

Still I do get this exception:

    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: 

I have tried handling this exception seperately, which didn't work. So I do not understand how I am supposed to handle it, as the code should just ignore the timeout, since it just means the element wasn't found (which is excpected in ~10% of the time).

Honn
  • 737
  • 2
  • 18

1 Answers1

1

An except clause with catch either the single exception or multiple exceptions, depending on what the expression following the keyword except evaluates to.

  1. If it evaluates to a single exception class, it will catch exceptions of that type.

  2. If it evaluates to a tuple, it will catch exceptions of any type contained in the tuple.

The expression ElementNotInteractableException or NoSuchElementException or TimeoutException evaluates to the single type ElementNotInteractableException, not a tuple containing the three exception classes.

You want an explicit tuple:

except (ElementNotInteractableException, NoSuchElementException, TimeoutException):
    ...
chepner
  • 497,756
  • 71
  • 530
  • 681