Stale element reference error in Selenium Scripting and how to solve it?
Stale element reference error and solution
What is Stale element reference error❓❓
The stale element reference error occurs when the referred element not attached to the DOM.
Every DOM element is represented in WebDriver by a unique identifying reference, known as a web element. The web element reference is a UUID used to execute commands targeting specific elements, such as getting an element’s tag name and retrieving a property off an element.
When an element is no longer attached to the DOM, i.e. it has been removed from the document or the document has changed, it is said to be stale. Staleness occurs for example when you have a web element reference and the document it was retrieved from navigates.
The Error Trace is:
org.openqa.selenium.StaleElementReferenceException: stale element
reference: element is not attached to the page document
Solution - 1:
Wait till the element is properly rendered and clickable, use below code:
WebDriverWait wait = new WebDriverWait(this.driver3, 15);
new WebDriverWait(this.driver3, 15).ignoring(StaleElementReferenceException.class).
until(ExpectedConditions.elementToBeClickable(id));
Solution - 2:
Ignore/catch the stale error:
try {
WebElement button = driver.findElement(By.xpath("xpath"));
button.click();
}
catch(org.openqa.selenium.StaleElementReferenceException ex)
{
WebElement button = driver.findElement(By.xpath("xpath"));
button.click();
}
Comments
Post a Comment