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?
CodePudding user response:
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:
- WebDriverWait for multiple conditions (OR logical evaluation)
- selenium two xpath tests in one
- Python / Selenium: Logic Operators in WebDriverWait Expected Conditions
CodePudding user response:
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) {
}