Reputation: 61
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();
}
Upvotes: 4
Views: 32953
Reputation: 1387
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.
Upvotes: 1
Reputation: 537
You can use implicit wait or sleep here
Implicit wait:
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Sleep
Thread.sleep(2000L);
in this case use throws InterruptedException
Hope it will help
Upvotes: 0
Reputation: 473763
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));
Upvotes: 13