Error StaleElementReferenceException with WebDriver

0 votes
I get a StaleElementReferenceException when I run my test: org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM.

Is there a way to resolve that exception? This comes in Grid, in which has a ref XPath expression that is dynamic.
Jun 21, 2018 in Selenium by eLiJha
• 770 points
1,165 views

1 answer to this question.

0 votes

I faced this exact problem once before. I figured out the sol. Hope the same helps you. 
So, I created a class to handle all the state elements depending on their type, cssselector, id, etc and then just called it like I any other page object.

public void StaleElementHandleByID (String elementID){
int count = 0;
boolean clicked = false;
while (count < 4 || !clicked)
{
    try {
       WebElement yourSlipperyElement= driver.findElement(By.id(elementID));
       yourSlipperyElement.click(); 
       clicked = true;
     } catch (StaleElementReferenceException e){
       e.toString();
       System.out.println("Trying to recover from a stale element :" + e.getMessage());
       count = count+1;
     }     
}

My advise is to use this only on elements you know will be problematic for WebDriver.

answered Jun 21, 2018 by nsv999
• 5,500 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Error with Selenium File Upload

In your code, you have used "WebElement" ...READ MORE

answered Mar 30, 2018 in Selenium by nsv999
• 5,500 points
6,167 views
0 votes
1 answer
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,494 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,031 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,146 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,816 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP