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

会有一股神秘感。 提交于 2019-12-30 13:12:53

问题


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.


回答1:


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"));



回答2:


The xpath looks valid. Use explicit wait to wait for visibility

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[(contains(@id,'idNumber1')) or (contains(@id,'idNumber2'))]")));



回答3:


Try this Xpath :-

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



回答4:


Please provide few seconds of wait before move to this code, so your driver may able to find the web-element.

Try this xpath

//*[contains(@id, 'idNumber1') or contains(@id, 'idNumber2')]


来源:https://stackoverflow.com/questions/45166181/how-to-wait-for-either-of-the-two-elements-in-the-page-using-selenium-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!