-1

I'm experiencing a StaleElementReferenceExpection that seems beyond all reason in an application. It involves absolutely no refreshing or reloading of the page, but just two consecutive actions of accessing the element - one click and one assertion that the element is not selected.

Java/Selenium code:

this.element.findElement(By.cssSelector("input[type='checkbox']")).sendKeys(Keys.SPACE);
(SPACE is used because when usng click, Selenium claims "ElementClickInterceptedExcpetion - other element would receive the click")

assertThat(this.element.findElement(By.cssSelector("input[type='checkbox']")).isSelected()).isFalse();

This results in:

org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

even when I use brake points int the code, and skip slowly through each step.

Markup:

<div class="hb-cell hb-cell--autoWidth">
 <input tusenskilletegn="" id="utgift-2-belop">
  <input type="checkbox" id="utgift-2-inkludert">
   <div class="hb-label>
    <label class="hb-label-tekst" for="utgift-2-inkludert"></label>
   </div>
  </div>

Also tried accessing the for" in the label, with no luck: (using partial match, in this example)

this.element.findElement(By.cssSelector("label[for$=-inkludert]")).click();

It results in

org.openqa.selenium.ElementNotInteractableException: element not interactable

Any ideas? Selenium seems to have become impossible for me to use lately, with this type of error occuring all "over the place".

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    after clicking (or sending space to select the checkbox) the DOM is updating.... during the updating you are trying to get the state of that checkbox, so you receive a stale element reference. Easy fix would be to just add a sleep after you send the space. Otherwise use a webdriverwait to find the element and catch Stale Element, re-getting the element if caught. (you need to re-find it since the reference is stale) – pcalkins Jun 07 '22 at 20:52
  • Does `label[for$=-inkludert]` identifies the desired element uniquely? – undetected Selenium Jun 07 '22 at 21:23

1 Answers1

0

If you have issues with the click() function you could always just click it using JS

public void clickWebElementUsingJS(WebElement element) {
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].click();", element);
}

As for the StaleElementReferenceExpection instead of adding an arbitrary Thread.sleep you could just write a little helper method using ExpectedConditions to click it and wait for the element to go stale, then just find the element again.

private WebElement clickCheckBoxAndWaitToGoStale() {
    final By byCSS = By.cssSelector("input#utgift-2-inkludert");
    // find the element
    WebElement checkbox = this.element.findElement(byCSS);

    // click it
    clickWebElementUsingJS(checkbox ); // or sendKeys(Keys.SPACE) if this doesn't work or you just prefer it

    // wait for the WebElement variable to go stale
    try {
        new FluentWait<>(driver)
                .withTimeout(Duration.ofSeconds(5))
                .pollingEvery(Duration.ofSeconds(1))
                .ignoring(StaleElementReferenceException.class)
                .ignoring(NullPointerException.class)
                .until(ExpectedConditions.invisibilityOf(element));
    } catch (WebDriverException e) {
        // log
    }
    // re-find and return the checkbox to run the assert on
    return this.element.findElement(byCSS);
}    
RJB
  • 31
  • 4