2

How to make Selenium WebDriver wait until combined expected condition?

Basically the similar question for Java has been asked and answered, but such method OR (docs) isn't here for Python binding (expected_conditions.py on GitHub)

I have a very slow callback that results in one of the following:

  • a div with id=_report_success if everything has been loaded correctly
  • a div with id=_report_error in case of any failure

So, I need to wait until either _report_success or _report_error becomes visible.

Separately these conditions are pretty straightforward:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, '_report_success')))

WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, '_report_error')))

In Java the combined version looks like the following:

driverWait.until(ExpectedConditions.or(
        ExpectedConditions.presenceOfElementLocated(...),
        ExpectedConditions.presenceOfElementLocated(...)
));

Of course, I can make a loop and check the presence of both with an interval (actually like it's implemented in WebDriverWait.until), but I'm looking for more elegant and flexible solution. After all, if the method for such needs is present in Java version, why it's not in a Python binding?

Oleh Rybalchenko
  • 6,998
  • 3
  • 22
  • 36

1 Answers1

3

You can use css_selector for OR

WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#_report_success, #_report_error')))
Guy
  • 46,488
  • 10
  • 44
  • 88
  • Thank you, definitely good solution for this particular case. But in general, we may need to combine different expected conditions. If there will be no answers with a general approach, I accept this one. – Oleh Rybalchenko Aug 07 '18 at 10:20