I am trying to learn the PageFactory model. I understood the fact that when we do a initElements
, the WebElements are located. Say for example, I click on a web
Stale element exception
is thrown in two cases
The element is no longer attached to the DOM
.
The element has been deleted entirely.
When this happen you wrap your code in try catch block
then you can loop and retry as many times as you need until it succeeds.
public void waitForElementPresent(final By by, int timeout){
WebDriverWait wait = (WebDriverWait)new WebDriverWait(driver,timeout)
.ignoring(StaleElementReferenceException.class);
wait.until(new ExpectedCondition(){
@Override
public Boolean apply(WebDriver webDriver) {
WebElement element = webDriver.findElement(by);
return element != null && element.isDisplayed();
}
});
}