afcbpete afcbpete - 1 year ago 108
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("MainContent_lblLandmarkUPRN")).getText();
assertEquals(s, actualValue);

Answer Source

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

String actualValue = element.getText();

You can also use textToBePresentInElement Expected Condition:

WebElement element = wait.until(ExpectedConditions.textToBePresentInElement("MainContent_lblLandmarkUPRN"), s));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download