-2

i am using Python Selenium Chromedriver and i am stuck at a problem. So i want to add different except Expectation to one line, like TimeoutExpectations and ElementClickInterceptedException to one. My current code:

        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()

and i want this part look for example like:

except TimeoutException, ElementClickInterceptedException:

How to get it work?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Yves1234
  • 35
  • 1
  • 7

2 Answers2

1

You can catch multiple exceptions in one line as following:

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

or slightly different syntax:

try:
    your_code_that_may_rise_exceptions
except (SpecificErrorOne, SpecificErrorTwo), e:
    handle(e)
Prophet
  • 32,350
  • 22
  • 54
  • 79
0

This can be achieved by inserting the following except statement:

except (TimeoutException, ElementClickInterceptedException):
Dominik
  • 3,612
  • 2
  • 11
  • 44
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179