I want use wait.until(ExpectedConditions)
with TWO elements.
I am running a test, and I need WebDriver
to wait until either of Element1 OR
Unfortunately, there is no such a command. You can overcome this by try and catch, or I would better recommend you to use open source Ruby library Watir.
You can also use a CSSSelector like this:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h2.someClass, h3.otherClass")));
Replace someClass and otherClass with what you had in [...] in the xpath.
If you have a variable number of conditions to satisfy, you can leverage a generic list such as Java's ArrayList<>
as shown here, and then do the following:
//My example is waiting for any one of a list of URLs (code is java)
public void waitUntilUrls(List<String> urls) {
if(null != urls && urls.size() > 0) {
System.out.println("Waiting at " + _driver.getCurrentUrl() + " for " + urls.size() + " urls");
List<ExpectedCondition<Boolean>> conditions = new ArrayList<>();
for (String url : urls) {
conditions.add(ExpectedConditions.urlContains(url));
}
WebDriverWait wait = new WebDriverWait(_driver, 30);
ExpectedCondition<Boolean>[] x = new ExpectedCondition[conditions.size()];
x = conditions.toArray(x);
wait.until(ExpectedConditions.or(x)); // "OR" selects from among your ExpectedCondition<Boolean> array
}
}
Now there's a native solution for that, the or
method, check the doc.
You use it like so:
driverWait.until(ExpectedConditions.or(
ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.something")),
ExpectedConditions.presenceOfElementLocated(By.cssSelector("div.anything"))));
There is an alternative way to wait but it isnt using expected conditions and it uses a lambda expression instead..
wait.Until(x => driver.FindElements(By.Xpath("//h3[@class='... ']")).Count > 0 || driver.FindElements(By.Xpath("//h2[@class='... ']")).Count > 0);
There is a simple solution for this, using an Explicit Wait:
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='FirstElement' or @id='SecondElement']")));
Before this I was trying to use wait.until(ExpectedConditions.or(...
, which was not compatible with selenium versions before 2.53.0.