wait.until(ExpectedConditions.visibilityOf Element

2020-02-08 19:58发布

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 Element2 shows up. Then I need to pick whoever shows up first. I've tried:

WebDriverWait wait = new WebDriverWait(driver, 60); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[@class='....']"))) || wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h3[@class='... ']"))); 

        // Then I would need:

String result = driver.findElement(By.xpath("...")).getText() || driver.findElement(By.xpath("...")).getText();

To sum up, I need to wait until either of the TWO elements shows up. Then pick whoever shows up (they cannot show up simultaneously) Please Help.

标签: java selenium
7条回答
▲ chillily
2楼-- · 2020-02-08 21:03

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.

查看更多
登录 后发表回答