Waiting for either elementID or simple alert in Selenium using Python

Viewed 17

In trying to automate a task in Selenium and python, I'm getting hung up constructing a wait statement looking for either an element OR an alert. I know I'm missing something.

After clicking an element, the result will either be a new page with a specific button on it (with a known "buttonID") or a simple alert will be generated. I want to capture and act on either of those events.

I'm able to wait for either button or the alert with:

driver.find_elements(By.ID,"buttonID")

and

expected_conditions.alert_is_present()

I can successfully combine looking for two elements using

WebDriverWait(driver,10).until(
lambda driver: driver.find_elements(By.ID,"buttonID1) or driver.find_elements(By.ID,"buttonID2"))
)

However, I'm having problems constructing a wait combining the find_elements and expected_conditions. Basically I'm trying to:

If buttonID is present:
   Do X
If a simple alert is present:
   Do Y

Wrap that in a try to capture any timeouts.

0 Answers
Related