Selenium: How to handle Stale Element Exception error
A stale element reference exception is thrown in one of two cases:
- The element has been deleted entirely or element/page has been refreshed
- The element is no longer attached to the DOM.
- The most simple solution is to use the Java PageFactory, this will create a proxy WebElement that will find the element every time you use it (There is a slim change that the element will be released in the couple of milliseconds between it being found and you performing an action on it, in this case it is suggested to use an explicit wait to wait for the element to become stale before finding it again).
- Use try catch to handle this exception and reload the element in catch block.
- Add a short sleep between FindElement & action method..