I've had a good look through here but the 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();
}
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));
You can use Explicit wait or Fluent Wait
Example of Explicit Wait -
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
WebElement aboutMe;
aboutMe= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("about_me")));
Example of Fluent Wait -
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(20, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement aboutMe= wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(By.id("about_me"));
}
});
Check this TUTORIAL for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With