gpt4 book ai didi

java - 获取陈旧元素引用 : element is not attached to the page document exception

转载 作者:行者123 更新时间:2023-12-02 04:43:03 25 4
gpt4 key购买 nike

在我的应用程序中,当我打开页面时,左侧显示选项卡列表。

默认情况下,一个选项卡处于打开状态,其他选项卡处于关闭状态,因此我正在查找打开的状态选项卡类名称并单击该选项卡,它已关闭,然后必须提供另一个选项卡 id 才能打开。

在执行代码时,我收到“过时元素引用:元素未附加到页面文档” 异常。

我也尝试过隐式等待选项。

有人可以帮忙解决这个问题吗?

driver.manage().timeouts().implicitlyWait(1000,TimeUnit.SECONDS);

WebElement element5 = driver.findElement(By.className("TopItemActive"));
if(element5.isEnabled())
{
element5.click();
}

driver.manage().timeouts().implicitlyWait(2000,TimeUnit.SECONDS);
WebElement element6 = driver.findElement(By.id("id_16_cell"));
element6.click();
System.out.println("Tab opened");

最佳答案

我的猜测是您的选项卡是使用 JavaScript 创建和删除的。 Webdriver 的作用是下载网页并将其存储在实例中。如果某些内容由于 javascript webdriver 而发生更改,则并不总是意识到它。

这可以作为一个简单的解决方案

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.refreshed(ExpectedConditions.elementToBeClickable(by)));

我发现对此没什么可做的。我捕获抛出的异常并重试。所以我为“点击”创建了一个新函数

public String click(By by){

String text = "";

driver.manage().timeouts().implicitlyWait( 5, TimeUnit.SECONDS );
boolean unfound = true;
int tries = 0;
while ( unfound && tries < 3 ) {
tries += 1;
try {
wait.until(ExpectedConditions.refreshed(ExpectedConditions.visibilityOfElementLocated(by)));
text = driver.findElement(by).click();
unfound = false;
logger.info("Click element "+stripBy(by));
} catch ( StaleElementReferenceException ser ) {
logger.error( "ERROR: Stale element exception. " + stripBy(by) );
} catch ( NoSuchElementException nse ) {
logger.error( "ERROR: No such element exception. " + stripBy(by)+"\nError: "+nse );
} catch ( Exception e ) {
logger.error( e.getMessage() );
}
}

if(unfound)
Assert.assertTrue(false,"Failed to locate element by locator " + stripBy(by));

return text;

}

关于java - 获取陈旧元素引用 : element is not attached to the page document exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29941204/

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