Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
871 views
in Technique[技术] by (71.8m points)

java - How to wait for either of the two elements in the page using selenium xpath

I have two elements I can wait for, I want to wait until either of them appears on the page.

I am trying to use xpath locator. But it is not working.

By.xpath("//*[(contains(@id,'idNumber1')) or (contains(@id,'idNumber2'))]"));

Is this achievable?
Please help me out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is possible to wait for one of two elements in the page using ExpectedConditions.or():

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.or(
    ExpectedConditions.elementToBeClickable(By.id("idNumber1")),
    ExpectedConditions.elementToBeClickable(By.id("idNumber2"))
)); 

You can also do an OR with a CSS selector using a comma ,:

wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#idNumber1, #idNumber2"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...