等待元素使用Selenium加载

afc*_*ete 2 java selenium selenium-webdriver

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

我是Java和Selenium的新手。我想尝试让代码中的元素等待超时。

有什么建议么?

到达这一点时,它崩溃了,因为该页面确实需要一段时间来搜索该地址。

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

ale*_*cxe 8

您需要使用WebDriverWait。例如,显式等待元素可见:

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

String actualValue = element.getText();
Run Code Online (Sandbox Code Playgroud)

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

WebElement element = wait.until(ExpectedConditions.textToBePresentInElement(By.id("MainContent_lblLandmarkUPRN"), s));
Run Code Online (Sandbox Code Playgroud)