2

I try to combine check for two scenarios:

If startupcheck fails we get a try again button:

el = WebDriverWait(self.driver, 10).until(
  EC.element_to_be_clickable((By.NAME, "Try again")))

Or startupcheck succeed we get a pin enter request in a custom object:

el = WebDriverWait(self.driver, 20).until(
  EC.element_to_be_clickable((By.XPATH, "//Custom/Edit")))

How can this be combined into one check without having to check for both: I tried the following:

check = WebDriverWait(self.driver, 20).until(
  EC.element_to_be_clickable(
    (By.XPATH, "//Custom/Edit") or (By.NAME, "Try again")
))

But only the first or statement is checked.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
MortenB
  • 2,749
  • 1
  • 31
  • 35

1 Answers1

5

You can club up combine check for both the elements using OR clause through a lambda expression as follows:

el = WebDriverWait(driver, 20).until(lambda x: (x.find_element_by_name("Try again"), x.find_element_by_xpath("//Custom/Edit")))

An alternative solution will be:

el = WebDriverWait(driver,20).until(lambda driver: driver.find_element(By.NAME,"Try again") and driver.find_element(By.XPATH,"//Custom/Edit"))

As an alternative you can club up combine check for both the elements using the equivalent as follows:

el = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='Try again'], Custom>Edit")))

References

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Fantastic, I wish this was in the official documentation on https://selenium-python.readthedocs.io/ – MortenB Sep 12 '19 at 07:44
  • Two questions: I'm unfamiliar with the term "club up using OR clause". what does that mean? Also, in the "alternative example" code, why is there an "and" instead of an "or" between the conditions? – rothloup Jan 12 '21 at 19:23
  • @rothloup Those are _lambda_ expressions to wait for expected conditions. For a simpler case see the discussion [Selenium's find_elements with two clauses for text?](https://stackoverflow.com/questions/65659942/seleniums-find-elements-with-two-clauses-for-text/65660325#65660325) – undetected Selenium Jan 12 '21 at 19:27
  • @DebanjanB: I know that they are lambda expressions. I don't understand what "club up" means (sounds like a colloquialism, google searches for that don't turn up much), nor why an "OR" condition is checked with an "AND". – rothloup Jan 12 '21 at 20:25