Search code examples
pythonseleniumselenium-webdriverwebdriverwebdriverwait

WebDriverWait for multiple conditions (OR logical evaluation)


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


Solution

  • 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: