Java和Selenium:Extreme StaleelementReferenceException

发布于 2025-02-05 19:27:31 字数 1267 浏览 1 评论 0原文

我正在遇到一种staleelementReference侵害,在应用程序中似乎超出了所有原因。它绝对不涉及页面的刷新或重新加载,而只需连续两个访问该元素的操作 - 一键单击和一个断言未选择该元素。

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();

这将导致:

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

即使我使用制动点在int code上,并缓慢跳过每个步骤。

标记:

<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>

尝试访问“标签中的for

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

org.openqa.selenium.ElementNotInteractableException: element not interactable

” “在这个地方”。

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".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2025-02-12 19:27:32

如果您在click()函数中遇到问题,则可以随时使用js单击它,

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

而不是添加任意线程。陈旧,然后再次找到元素。

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);
}    

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);
}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文