180

I have list which has multiple links under each section. Each section has same links I need to click a particular link under each section. I have written the below code but when it executes it gives me stale element reference: element is not attached to the page document error.

This is my code:

public static void main(String[] args) throws InterruptedException 
{
    WebDriver driver = new ChromeDriver();
    driver.navigate().to("url......");
        driver.findElement(By.id("Login1_txtEmailID")).sendKeys("[email protected]");
    driver.findElement(By.id("Login1_txtPassword")).sendKeys("Testing1*");
    driver.findElement(By.id("Login1_btnLogin")).click();
    List<WebElement> LeftNavLinks=driver.findElements(By.xpath("//*[@id='sliding-navigation']//a"));
    Thread.sleep(1000);
    String ben="Benefit Status";
    String[] linkTexts = new String[LeftNavLinks.size()];
    int i = 0;
    for (WebElement e : LeftNavLinks) 
    {   
        linkTexts[i] = e.getText();
        System.out.print(i+" " + linkTexts[i]+"\n");
        if(linkTexts[i].equals(ben))
        {
            String BenefitStatLi="//*[@id='sliding-navigation']/li[%s]/a";
            System.out.print(i+" " + linkTexts[i]+"\n");
                driver.findElement(By.xpath(String.format(BenefitStatLi,i))).click();
            driver.findElement(By.xpath("//* [@id='divContentHolder']/div[1]/a[1]")).click();
        }
        i++;
    }
}

}

This is the HTML structure is as below

<div id="ucAdminMenu_divMenu">
  <ul id="sliding-navigation">
    <li class="sliding-element">
      <a href=" ">Claims Status</a>
    </li>
    <li class="sliding-element">
      <a href=" ">Eligibility Status</a>
    </li>
    <li class="sliding-element">
      <h3>Section-1</h3>
    </li>
    <li class="sliding-element">
      <a href=" ">Forms and Documents</a>
    </li>
    <li class="sliding-element">
      <a href=" HourBank.aspx?id=002">Hour Bank</a>
    </li>
    <li class="sliding-element">
      <h3>Section-2</h3>
    </li>
    <li class="sliding-element">
      <a href=" ">Benefit Status</a>
    </li>
    <li class="sliding-element">
      <a href=" ">Forms and Documents</a>
    </li>
    <li class="sliding-element">
      <h3>Section-3</h3>
    </li>
    <li class="sliding-element">
      <a href=" ">Forms and Documents</a>
    </li>
    <li class="sliding-element">
      <h3>Testing Fund</h3>
    </li>
    <li class="sliding-element">
      <a href=" ">Benefit Status</a>
    </li>
    <li class="sliding-element">
      <a href=" ">Order ID Card</a>
    </li>
  </ul>
</div>

The Error Trace is:

    Exception in thread "main" 
org.openqa.selenium.StaleElementReferenceException: stale element 
reference: element is not attached to the page document

15 Answers 15

131

What is the line which gives exception ??

The reason for this is because the element to which you have referred is removed from the DOM structure

I was facing the same problem while working with IEDriver. The reason was because javascript loaded the element one more time after i have referred so my date reference pointed to a nonexistent object even if it was right there in the UI. I used the following workaround:

try {
    WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
    date.click();
}
catch(org.openqa.selenium.StaleElementReferenceException ex)
{
    WebElement date = driver.findElement(By.linkText(Utility.getSheetData(path, 7, 1, 2)));
    date.click();
}

See if the same can help you !

6
  • linkTexts[i] = e.getText(); lines gives me error while looping for the second time Commented Aug 14, 2013 at 8:15
  • 1
    Resolved the error was due to page refresh 1. first i copied all the link texts to a String array variable and later used to for loop to click on the required links under each section. for (WebElement e : LeftNavLinks) { linkTexts[i] = e.getText(); i++; } for (int j=0;j<leftnavlen;j++) { if(linkTexts[j].equals(ben)) { String BenefitStatLi="//*[@id='sliding-navigation']/li[%s]/a"; driver.findElement(By.xpath(String.format(BenefitStatLi,j+1))).click(); driver.findElement(By.xpath("//*@id='divContentHolder']/div[1]/a[1]")).click(); } Commented Aug 14, 2013 at 8:59
  • So it was for the same same reason. Elements identified earlier were removed from DOM due to page refresh :) Commented Aug 14, 2013 at 9:06
  • In my case i got this exception as i do not have break statement in the loop after the required element is clicked.So watch out for such situations as well.
    – Raj Asapu
    Commented Apr 3, 2017 at 18:30
  • 1
    You're absolutely right, although the targeted element is displayed and clickable, it was reloaded in the DOM (I had a case of autocomplete text field), your idea is working great with some exception handling mechanism...thanks a lot !
    – Sentary
    Commented Jun 21, 2022 at 14:49
97

Whenever you face this issue, just define the web element once again above the line in which you are getting an Error.

Example:

WebElement button = driver.findElement(By.xpath("xpath"));
button.click();

//here you do something like update or save 

//then you try to use the button WebElement again to click 
button.click();

Since the DOM has changed e.g. through the update action, you are receiving a StaleElementReference Error.

Solution:

WebElement button = driver.findElement(By.xpath("xpath"));
button.click();

//here you do something like update or save 

//then you define the button element again before you use it
WebElement button1 = driver.findElement(By.xpath("xpath"));
//that new element will point to the same element in the new DOM
button1.click();

5
  • Hey, this seem to have solved my error. But I do not understand why I have to define same element having defined it earlier.
    – Abhi
    Commented Jul 29, 2020 at 22:36
  • Great help. But can you please explain why the DOM changed for the same variable? Thanks.
    – JuBaer AD
    Commented Nov 26, 2020 at 0:46
  • @JuBaerAD -- Would assert it can completely on the framework (react/vue/others). Simple "direct" HTML would be hard to see things changing. But using a framework it might do something completely for its' own and the side effect is this. Wrong? First and foremost does the UI still work i.e. the React UI still work and function as needed? Yes, no problem... I've does UI testing for 20+ years, i've seen UIs where there were only actual controls upon referencing a control (fun to test not), and every time you accessed technically got a new control.. Same thing here i'd suspect...
    – Dano
    Commented Dec 4, 2020 at 21:16
  • 2
    This one solved my issue, also if you are using the Actions class Click() method for click that element you can not use the same action class object for every click, you have to recreate objects from the action class every time when clicking on particular element otherwise it will return the StaleElementReference error Commented Aug 10, 2021 at 9:49
  • 1
    Seriously ? 91 upvotes? wow! Commented Jan 16, 2023 at 12:09
26

This errors have two common causes: The element has been deleted entirely, or the element is no longer attached to the DOM.

If you already checked if it is not your case, you could be facing the same problem as me.

The element in the DOM is not found because your page is not entirely loaded when Selenium is searching for the element. To solve that, you can put an explicit wait condition that tells Selenium to wait until the element is available to be clicked on.

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, 'someid')))

See: https://selenium-python.readthedocs.io/waits.html

In Java

import org.openqa.selenium.support.ui.ExpectedConditions;

WebDriverWait wait = new WebDriverWait(driver, 10);
element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));

See: https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html

2
  • this worked for me. Also link to the document was helpful in understanding how it works. Thanks @Bruno_Sanches Commented Apr 29, 2020 at 12:20
  • Thanks for this answer. WebDriverWait was giving me issues but I used time.sleep(10) and it fixed the issue. Perhaps not the best solution but still worked. Commented Jan 21, 2021 at 19:58
12

To handle it, I use the following click method. This will attempt to find and click the element. If the DOM changes between the find and click, it will try again. The idea is that if it failed and I try again immediately the second attempt will succeed. If the DOM changes are very rapid then this will not work.

public boolean retryingFindClick(By by) {
    boolean result = false;
    int attempts = 0;
    while(attempts < 2) {
        try {
            driver.findElement(by).click();
            result = true;
            break;
        } catch(StaleElementException e) {
        }
        attempts++;
    }
    return result;
}
6

Just break the loop when you find the element you want to click on it. for example:

  List<WebElement> buttons = getButtonElements();
    for (WebElement b : buttons) {
        if (b.getText().equals("Next"){
            b.click();
            break;
        }
5

The thing here is that you're using a for loop outside of your conditional statement.

After the conditions in your IF statement are met you probably navigate to another page, thus when the for loop attempts to iterate once more, you get the stale element error because you're on a different page.

You can add a break at the end of your if statement, this worked for me.

1
  • Took me a while to understand why error was occuring!
    – BEWARB
    Commented Dec 8, 2019 at 14:11
4
try {
    WebElement button = driver.findElement(By.xpath("xpath"));
            button.click();
}
catch(org.openqa.selenium.StaleElementReferenceException ex)
{
    WebElement button = driver.findElement(By.xpath("xpath"));
            button.click();
}

This try/catch code actually worked for me. I got the same stale element error.

2
  • 2
    Is this answer somehow different from the top-voted answer here?
    – SiKing
    Commented Jun 25, 2018 at 21:03
  • @Charan Raz I have refactored the code a little. For just sake of doing the task, it is good. But this is not a recommended approach to handling exception. Commented Jun 2, 2022 at 8:37
3

Use this code:

public class LinkTest 
{   
    public static void main(String[] args) 
    {
        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("file:///C:/Users/vkiran/Desktop/xyz.html");
        List<WebElement> alllinks =driver.findElements(By.xpath("//*[@id='sliding-navigation']//a"));
        String a[]=new String[alllinks.size()];
        for(int i=0;i<alllinks.size();i++)
        {
            a[i]=alllinks.get(i).getText(); 
            if(a[i].startsWith("B"))
            {
                System.out.println("clicking on this link::"+driver.findElement(By.linkText(a[i])).getText());
                driver.findElement(By.linkText(a[i])).click();  

            }
            else
            {
                System.out.println("does not starts with B so not clicking");
            }
        }
}
}
2

This could be done in newer versions of selenium in JS( but all supporting stalenessOf will work):

 const { until } = require('selenium-webdriver');
 driver.wait(
        until.stalenessOf(
          driver.findElement(
            By.css(SQLQueriesByPhpMyAdminSelectors.sqlQueryArea)
          )
        ),
        5 * 1000
      )
      .then( driver.findElement(By.css(SQLQueriesByPhpMyAdminSelectors.sqlQueryArea))
      .sendKeys(sqlString)
  );
1

I had this issue several times. In one of my cases the accepted answer didn't solved my issue as for me the problem was that the center of the element was not clickable and the thrown exception is misleading "stale element reference: element is not attached to the page document".

For me the fix was to scroll to the element to make sure the center of the element was clickable.

To do this I used the following code:

WebElement yourElement=
                driver.findElement(By.id("yourElementId"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", yourElement);
yourElement.click();

Hope this helps!

0

According to @Abhishek Singh's you need to understand the problem:

What is the line which gives exception ?? The reason for this is because the element to which you have referred is removed from the DOM structure

and you can not refer to it anymore (imagine what element's ID has changed).

Follow the code:

class TogglingPage {
  @FindBy(...)
  private WebElement btnTurnOff;

  @FindBy(...)
  private WebElement btnTurnOn;

  TogglingPage turnOff() {
    this.btnTurnOff.isDisplayed();  
    this.btnTurnOff.click();          // when clicked, button should swap into btnTurnOn
    this.btnTurnOn.isDisplayed();
    this.btnTurnOn.click();           // when clicked, button should swap into btnTurnOff
    this.btnTurnOff.isDisplayed();    // throws an exception
    return new TogglingPage();
  }
}

Now, let us wonder why?

  1. btnTurnOff was found by a driver - ok
  2. btnTurnOff was replaced by btnTurnOn - ok
  3. btnTurnOn was found by a driver. - ok
  4. btnTurnOn was replaced by btnTurnOff - ok
  5. we call this.btnTurnOff.isDisplayed(); on the element which does not exist anymore in Selenium sense - you can see it, it works perfectly, but it is a different instance of the same button.

Possible fix:

  TogglingPage turnOff() {
    this.btnTurnOff.isDisplayed();  
    this.btnTurnOff.click();

    TogglingPage newPage = new TogglingPage();
    newPage.btnTurnOn.isDisplayed();
    newPage.btnTurnOn.click();

    TogglingPage newerPage = new TogglingPage();
    newerPage.btnTurnOff.isDisplayed();    // ok
    return newerPage;
  }
0

In my case, I had a page where it was an input type='date' whose reference I had got on page load, but When I tried to interact with it, it showed this exception and that was quite meaningful as Javascript had manipulated my control hence it was detached from the document and I had to re-get its reference after the javascript had performed its job with the control. So, this is how my code looked before the exception:

if (elemDate != null)
{ 
    elemDate.Clear(); 
    elemDate.SendKeys(model.Age);
}

Code after the exception was raised:

int tries = 0;
do
{
    try
    {
        tries++;
        if (elemDate != null)
        {
            // these lines were causing the exception so I had break after these are successfully executed because if they are executed that means the control was found and attached to the document and we have taken the reference of it again.
            elemDate.Clear();
            elemDate.SendKeys(model.Age);
            break;
        }
    }
    catch (StaleElementReferenceException)
    {
        System.Threading.Thread.Sleep(10); // put minor fake delay so Javascript on page does its actions with controls
        elemDate = driver.FindElement(By.Id(dateId));
    }
} while (tries < 3); // Try it three times.

So, Now you can perform further actions with your code or you can quit the driver if it was unsuccessful in getting the control to work.

if(tries > 2)
{
   // element was not found, find out what is causing the control detachment.
   // driver.Quit();
   return;
}

// Hurray!! Control was attached and actions were performed.
// Do something with it...

Something that I have learnt so far is, catching exceptions to know about successful code execution is not a good idea, But, I had to do it and I found this work-around to be working well in this case.

PS: After writing all this, I just noticed the tags that this thread was for java. This code sample is just for demonstration purpose, It might help people who have issue in C# language. Or it can be easily translated to java as it doesn't have much C# specific code.

0

In my case, the locator I was using has multiple results thus it was unable to identify the right element to perform an action eventually exception was being thrown. So having a locator which is unique, resolved my problem.

-1

In my case the problem was resolved simply by a WAITER!

 public void ClickOnButton()
 {
        Driver.WaitForPageUntilIWebElementIsClickable(Button);
        Button.Click();
 }
-9

use this code to wait till the element is attached:

boolean breakIt = true;
        while (true) {
        breakIt = true;
        try {
            // write your code here
        } catch (Exception e) {
            if (e.getMessage().contains("element is not attached")) {
                breakIt = false;
            }
        }
        if (breakIt) {
            break;
        }

    }
6
  • 1
    didn't work for me but still an interesting approach
    – Yar
    Commented Jun 3, 2016 at 19:25
  • 32
    This makes no sense what so ever.
    – Sam
    Commented Aug 26, 2016 at 20:02
  • 1
    It makes sense, it's just not the most clear in terms of its variable name "breakIt". What that does is break out of the while(true) loop as soon as the "element is not attached" error is no longer being thrown.
    – emery
    Commented Apr 18, 2017 at 16:30
  • 'boolean doIt = true; while (doIt) { try { // write your code here } catch (Exception e) { if (e.getMessage().contains("element is not attached")) { doIt = false; } } }'
    – Tao Zhang
    Commented May 30, 2017 at 16:44
  • 3
    smells like goto spirit Commented Dec 5, 2018 at 2:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.