StaleElementRefereException occures when element was fetched before you navigate from that page or refresh the page. Also, it happnes sometimes while working with Ajax calls.
e.g.
List<WebElement> elements = driver.findElements(By.tagName("tr")); // this will return all tr (table rows in the page)
while(WebElement element : elements){
driver.navigate().refresh(); //I am refreshing the page.
element.click(); // this line will throw StaleElementRefereException
}
To overcome above error, we usually refetch the element everytime after completing refresh or page navigation.
eg.
List<WebElement> elements = driver.findElements(By.tagName("tr"));
for(int i=0; i < elements.size(); i++){
driver.navigate().refresh(); //I am refreshing the page.
elements = driver.findElements(By.tagName("tr")); //I refetch all elements
elements.get(i).click(); //this will work
}
Hope that helps! Reply to this comment with your code and exact exception you are getting, so that I can help you with better solution.