Stale element reference: element is not attached to the page of the document



  • 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");
    
            startingNo.sendKeys(Integer.toString(i));
    
            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>
    </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.



  • 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)
                .ignoring(StaleElementReferenceException.class)
                .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");
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2