gpt4 book ai didi

java - 如何处理过时的元素引用: element is not attached to the page document

转载 作者:行者123 更新时间:2023-12-02 08:51:29 25 4
gpt4 key购买 nike

我有一个应用程序,我必须等待按钮在页面上可见并单击它。该按钮具有 id="next-arrow-button",但在父 div 上它具有 id="recorder-0"

enter image description here

按下此按钮后,该按钮将在几秒钟内不再显示。

按钮再次显示如下: enter image description here

您可以看到它具有相同的 id ( id="next-arrow-button"),但在父 div 上它有另一个 id ( id="recorder-1",之前这是重新编码器-0)

我等待元素可见并尝试单击它,但收到此错误:

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

我的代码有点乱,但我想与您分享它的一个版本:

 WebElement firstRecording = new WebDriverWait(driver, Constant.TIMEOUT_SECOND).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#recorder-0 #next-arrow-button")));
firstRecording.click();


new WebDriverWait(driver, Constant.TIMEOUT_SECOND).until(ExpectedConditions.stalenessOf(firstRecording));



WebElement secondRecording = new WebDriverWait(driver, Constant.TIMEOUT_SECOND).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#recorder-1 #next-arrow-button")));
secondRecording.click();

我尝试删除该行:

new WebDriverWait(driver, Constant.TIMEOUT_SECOND).until(ExpectedConditions.stalenessOf(firstRecording));

但我遇到了同样的错误。

我是初级学生,如果我犯了一个愚蠢的错误,我很抱歉,但我真的需要你的帮助。谢谢:)

最佳答案

我在这里找到了解决方案。只需忘记 selenium 的等待并使用 Java 代码即可:

 public void customWaitAndClick(WebDriver driver, String CSSelement) {
boolean flag = true;
long currentTime = System.currentTimeMillis();
long end = currentTime + 60000L;

while (!flag || System.currentTimeMillis() < end) {
try {
driver.findElement(By.cssSelector(CSSelement)).click();
break;
} catch (Exception e) {
flag = false;
}
}
}

关于java - 如何处理过时的元素引用: element is not attached to the page document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60737907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com