等待元素使用Selenium加载 [英] Waiting for an element to load using Selenium

查看:99
本文介绍了等待元素使用Selenium加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里进行了很好的浏览,但是等待的web元素似乎不适合我的代码.

I've had a good look through here but the web element waits don't seem to fit into my code.

我是Java和Selenium的新手.我想尝试在代码超时之前等待它进入我的代码.

I'm fairly new to Java and Selenium. I want to try and get a wait for element into my code before it times out.

有什么建议吗?

到达这一点时,它会崩溃,因为该页面确实需要一段时间才能搜索到该地址.

It's crashing when it hits this point because the page does take a while to search for this address.

@Step ("Verifying landmark")
public void validatingLandmark(String s){
    String actualValue = getDriver().findElement(By.id("MainContent_lblLandmarkUPRN")).getText();
    assertEquals(s, actualValue);
    TimeOut();
}

推荐答案

您需要使用

You need to use WebDriverWait. For instance, explicitly wait for an element to become visible:

WebDriverWait wait = new WebDriverWait(getDriver(), timeOut);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("MainContent_lblLandmarkUPRN")));

String actualValue = element.getText();

您还可以使用textToBePresentInElement预期条件:

You can also use textToBePresentInElement Expected Condition:

WebElement element = wait.until(ExpectedConditions.textToBePresentInElement(By.id("MainContent_lblLandmarkUPRN"), s));

这篇关于等待元素使用Selenium加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆