1

After I click a button on a webpage, one of two things can happen. Normally, I would use a wait until when there's a single event outcome, but is there any built in methodology where I can wait until 1 of two things happens i.e. one of two elements exists?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
asdasd31
  • 95
  • 7

2 Answers2

4

To wait until either of two elements you can induce WebDriverWait for either of the two elements through the OR option and you can use either of the following approaches:

  • Using CssSelector you can pass the expressions seperated by comma as follows:

    element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".classA, .classB"))
    
  • Using CssSelector and you can pass the expressions through OR condition as follows:

    element = WebDriverWait(driver,20).until(lambda driver: driver.find_element(By.CSS_SELECTOR, "tagname.classname") or driver.find_element(By.CSS_SELECTOR, "tagname#elementID"))
    
  • Using XPath you can pass the expressions through OR condition as follows:

    element = WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH, "//tag_name[@class='elementA' or @id='elementB']"))
    
  • Using XPath and you can pass the expressions through OR condition as follows:

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

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

you can do it tbh this is in java i dont know about python but this is at least an idea if you have two condition to be met for example input 1 to be present and input 2 I would create two boolean variabels that i will put in them driver.findelements(by....).size()>0; then i will just add an if so in case not both of them show it will crash or do what ever i want. Code example:

    Boolean AccesViaLogin = driver.findElements(By.id("username_login")).size() > 0;
    Boolean AccesViaHomepage = driver.findElements(By.xpath("//button[contains(text(),\"Connexion\")]")).size() > 0;
    if (AccesViaLogin == true && AccesViaHomepage == true) {
}
Yan
  • 108
  • 1
  • 8