Vishnu
Vishnu

Reputation: 2452

Check if either of element is located

I wanted to check , if element1 or element2 is present then return true.I use following code to check if element 1 is present.Now I want to check if either element with class name element1 or element2 is present, it should return true and finish the wait condition and move on to next line

driver.wait(until.elementLocated(by.className('element1')), 10000);

Basically something like below ? :P

driver.wait(until.elementLocated(by.className('element1 || element2')), 10000);

My example DOM looks like below

<div class="element1"></div>

OR 

<div class="element2"></div>

if anyone of div present, My coditions will be met..sometimes webpage generate only element 1 , sometimes it generate only element2, or sometimes both.

Upvotes: 0

Views: 334

Answers (3)

rooaarr
rooaarr

Reputation: 1

Finally got a solution that worked in Javascript. Many thanks to Moe for pointing in the right direction.

await driver.wait(until.elementLocated(By.css('.element1, .element2')));

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193188

Here is the solution to your Question-

In Selenium 3.4.0 to induce ExplicitWait you can use multiple clauses with ExpectedConditions as follows:

Java

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.or(
        ExpectedConditions.visibilityOfElementLocated(By.className("element1")),
        ExpectedConditions.visibilityOfElementLocated(By.className("element2"))));

Let me know if this Answers your Question.

Upvotes: 1

Moe Ghafari
Moe Ghafari

Reputation: 2267

You can do this by using a css selector like this:

by.cssSelector("div.element1, div.element2") 

The comma is an OR operator in this case.

Upvotes: 2

Related Questions