Contents
When does selenium throw a stale element reference exception?
First of all lets be clear about what a WebElement is. A WebElement is a reference to an element in the DOM. A StaleElementException is thrown when the element you were interacting is destroyed and then recreated.
How often should selenium check for an element?
// Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds.
What does a stale element in WebDriver mean?
Stale means old, decayed, no longer fresh. Stale Element means an old element or no longer available element. Assume there is an element that is found on a web page referenced as a WebElement in WebDriver. If the DOM changes then the WebElement goes stale.
How to prevent an element from becoming stale?
To prevent an element to become stale, place a new element on the current page, hit the link and wait until the element is not available anymore. Then wait for an element on the new page to appear
What happens when an element goes stale in WebDriver?
An element that is found on a web page referenced as a WebElement in WebDriver then the DOM changes (probably due to JavaScript functions) that WebElement goes stale. The element has been deleted entirely. When you try to interact with the staled WebElement [any above case], the StaleElementException is thrown.
When do you get a webelement reference exception?
A WebElement is a reference to an element in the DOM. A StaleElementException is thrown when the element you were interacting is destroyed and then recreated. Most complex web pages these days will move things about on the fly as the user interacts with it and this requires elements in the DOM to be destroyed and recreated.
How to avoid staleelementreferenceexception in JavaScript?
Use ExpectedConditions.refreshed to avoid StaleElementReferenceException and retrieve the element again. This method updates the element by redrawing it and we can access the referenced element.