6

Using python, the method WebDriverWait is used to wait for 1 element to be present on the webpage. How can this method be used without multiple try/except? Is there an OR option for multiple cases using this method? https://selenium-python.readthedocs.io/waits.html

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
dirtyw0lf
  • 1,899
  • 5
  • 35
  • 63
  • 3
    you can use OR in XPATH locators. The ExpectedCondition always returns a single bool. What is the use-case? – pcalkins Mar 02 '20 at 20:46

1 Answers1

10

Without using multiple try/except{} to induce WebDriverWait for two elements through OR option you can use either of the following solutions:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".element_A_class, .element_B_class"))
    
  • Using XPATH through :

    element = WebDriverWait(driver,20).until(lambda driver: driver.find_element(By.XPATH,"element_A_xpath") or driver.find_element(By.XPATH,"element_B_xpath"))
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • The multi-match `By.xpath` worked for me. It is a little odd, but it works. P.S. This is Selenium-general, not Python-specific. – MarkHu Oct 07 '21 at 19:25