afcbpete afcbpete - 1 month ago 6
Java Question

Waiting for an element to load using Selenium

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

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.

Any suggestions?

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();
}

Answer

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

You can also use textToBePresentInElement Expected Condition:

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