I would really appreciate if someone could help figure out what the hell is going on with one of my tests.

I'm getting Stale element reference: element is not attached to the page of the documenterror. I'd really like to crack this one since there are a only a few simple steps in the test.

startingNo = Driver.driver.findElement(By.xpath("//*[@id=\"centralForm:startSerialNumber_input\"]"));

        System.out.println("Trying to .sendkeys");


        System.out.println("Keys sent");

It seems that Selenium has found my element, but it doesn't get to write anything to that input field. This the HTMLof that section:

<div class="ui-g-12 ui-md-6 ui-lg-3">
<span id="centralForm:startSerialNumber" class="ui-inputnumber ui-widget wh-full-width-container">
<input id="centralForm:startSerialNumber_input" name="centralForm:startSerialNumber_input" type="text" value="0" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" aria-required="true" aria-labelledby="centralForm:startSerialNumberLabel" data-p-label="First serial number" data-p-rmsg="input is required" data-p-con="javax.faces.Long" data-p-required="true" data-p-hl="inputnumber" role="textbox" aria-disabled="false" aria-readonly="false">
<input id="centralForm:startSerialNumber_hinput" name="centralForm:startSerialNumber_hinput" type="hidden" autocomplete="off" data-p-label="First serial number" data-p-rmsg="input is required" data-p-con="javax.faces.Long" data-p-required="true" data-p-hl="inputnumber" value="0"></span>
<div id="centralForm:j_idt94" aria-live="polite" data-display="both" data-target="centralForm:startSerialNumber" data-redisplay="true" class="ui-message"></div>

I never made it to "Keys sent" System.out.println. The element is visible to the naked eye at all times, and I can locate it using its xpath in the Inspector at all times.

Error I get is

FAILED: ImportBlankPassportsToProduction 
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
(Session info: chrome=76.0.3809.132)

I've tried implementing WebDriverWait for element to be clickable, and I've tried visible as well. But no luck. I'm kinda stuck :/

I'd be willing to try it all over again with tips from you.

  • Is the page you're runing your test against publicly available? There can be the case that javascript that is running on the page, changes the element position inside DOM continuously. It is better to test on real example. I'm afraid plain html wouldn't help much. – Alexey R. Sep 24 '19 at 9:23
  • Unfortunately its not a publicly available page. How can I check if JavaScript is causing the issue? – Nikola Pepic Sep 24 '19 at 10:09

There is no straightforward solution for something like this, since it is a content specific problem. StaleElementReferenceException, according to Selenium docs, is thrown in one of two cases:

A stale element reference exception is thrown in one of two cases, the first being more common than the second:

  1. The element has been deleted entirely.
  2. The element is no longer attached to the DOM.

We can assume your element has not been deleted as you can see it, so we are dealing with the second case. This usually means that the page is reloading in between you locating the element and your interaction with the element. To debug this - put a breakpoint on the Driver.driver.findElement... line and wait some time - so that the page fully loads, then resume - if it works you need to implement a waiting mechanism (preferably a FluentWait) so that your WebElement does not change in between location and interaction. Here is a some sample code that uses Java 8 lambda and FluentWait:

    Wait<WebDriver> fluentWait = new FluentWait<>(driver)
            .pollingEvery(Duration.of(500, MILLIS))
            .withTimeout(Duration.of(10, SECONDS))
            .withMessage("not found");
    WebElement element = fluentWait.until(webDriver -> webDriver.findElement(By.xpath("...")));
    element.sendKeys("some text");
| improve this answer | |
  • java.lang.NullPointerException: unit is the error I'm getting. – Nikola Pepic Sep 24 '19 at 12:34
  • I have changed the polling to 'MILLIS'. Regarding the nullPointer - you may be missing this imports: import static java.time.temporal.ChronoUnit.MILLIS; import static java.time.temporal.ChronoUnit.SECONDS; – Moro Sep 24 '19 at 12:46

Try actions. And also rather creating varaible, recall element again while call send keys, such as

Actions myaction= new Actions(Driver.driver);
myaction.sendKeys(Driver.driver.findElement(By.xpath("//*@id=\"centralForm:startSerialNumber_input\"]")), Integer.toString(i))
| improve this answer | |
  • Hi Veena, could you provide some sample code that better illustrates what you're suggesting? Thanks! – corsiKa Sep 25 '19 at 6:10
  • Actions myaction= new Actions(Driver.driver); myaction.sendKeys(Driver.driver.findElement(By.xpath("//*[@id=\"centralForm:startSerialNumber_input\"]")), Integer.toString(i)).build().perform(); – Veena Devi Sep 26 '19 at 4:31

Changes in the webpages will cause this type of error. In order to solve that:

Try to put it in front of the function that performs the extraction

At the start of the for loop if you have one

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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