Rather than storing references to elements, it is important to locate and interact with them dynamically to avoid the StaleElementReferenceException. To do this, one must use a combination of explicit waits and polling to locate elements. Then, the element must be interacted with immediately after locating it to avoid the exception. This can be done by using the appropriate methods available in the Selenium WebDriver API.

Code example one:
Try to refresh the page before send the keys.


Code example two:
Use try catch and in catch block try to initiate a variable.

  try {
    System.out.println("method f id:"+Thread.currentThread().getId()+" n:"+n+" s:"+s);
}catch(StaleElementReferenceException e) {

Support On Demand!

QA Automation