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
• 720 points
41 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,090 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 29, 2018 in Selenium by nsv999
• 5,090 points
967 views
0 votes
1 answer
0 votes
1 answer

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,090 points
555 views
0 votes
1 answer
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,130 points
890 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
553 views
0 votes
1 answer