There is such a situation where I find inner div
but with varying success, because of StaleElementReferenceException
.
<div role="dialog" aria-modal="true">
<div class="DialogBody"></div>
</div>
At firt I locate dialog
div and then try to avoid StaleElementReferenceException
when find inner div
WebElement dialog = null;
try {
dialog = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[role='dialog']")));
} catch (TimeoutException e) {
logger.severe("no dialog after login");
}
logger.info("dialog found");
waitForChat(30);
try {
WebElement div = retryingFind(By.tagName("div"), dialog);
logger.info(div.getAttribute("class")); //null because the div is null
logger.info("inner div found");
} catch (TimeoutException e) {
logger.severe(ExceptionUtils.getStackTrace(e));
}
where retryingFind method:
public WebElement retryingFind(By by, WebElement el) {
WebElement result = null;
int attempts = 0;
while(attempts < 20) {
try {
result = el.findElement(by);
break;
} catch(StaleElementReferenceException e) {
System.out.println("StaleElementReferenceException");
}
attempts++;
}
return result;
}
Sometimes the inner div is located without problems, but StaleElementReferenceException
arbitrarily occurs when finding an element.
As far as I understand the StaleElementReferenceException
indicates that the reference to an element is now stale i.e. the element no longer appears within the HTML DOM of the page, but the element is visible at the moment of detecting it.
It is also recommended to check the element on elementToBeClickable(), but firstly does this make sense for a div, and secondly I find this div through another element, and not through the webdriver, so the constructor new WebDriverWait(WebElement el, Duration)
is undefined.
How to solve this situation?
Since the page is dynamic, something was causing div role ='dialog'
to change, so the solution is to update this external div as follows:
public WebElement retryingFind(By by, WebElement el) {
WebElement result = null;
int attempts = 0;
while(attempts < 5) {
try {
result = el.findElement(by);
break;
} catch(StaleElementReferenceException e) {
try {
el = driver.findElement(By.cssSelector("div[role='dialog']"));
} catch (NoSuchElementException e1) {}
}
attempts++;
}
return result;
}
Thus, I have never ever encountered this problem again.