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?

- 183,867
- 41
- 278
- 352

- 95
- 7
-
3Xpath | or statements would be what your looking for. Driver.find_element_by_xpath(" x | y ") – Arundeep Chohan Nov 19 '21 at 21:55
2 Answers
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 lambda 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 lambda 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:

- 183,867
- 41
- 278
- 352
-
1Just adding a point. If both elements are having same tag then `xPath` can be written like this `//div[@id='PEGA_GRID_CONTENT' or @gpropindex='PNewDetails1']` as well – Nandan A Nov 25 '21 at 12:50
-
1
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) {
}

- 108
- 1
- 8