- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,当我打开页面时,左侧显示选项卡列表。
默认情况下,一个选项卡处于打开状态,其他选项卡处于关闭状态,因此我正在查找打开的状态选项卡类名称并单击该选项卡,它已关闭,然后必须提供另一个选项卡 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/
我不明白文档 here 对我来说,令人困惑的一点是:为什么要在响应上设置 etag 或 last_modified?客户端不是发送 etag 和 if-modified-since header 的人
所以我使用 Selenium 抓取链接。我可以用循环打印我的链接,但我无法导航到它们,因为我收到以下错误: selenium.common.exceptions.StaleElementReferen
这是一个非常奇怪的问题。今天 AS (1.4) 停止在我的项目中正确地重新创建调试 apk: 运行项目时,任何XML 更改都不会反射(reflect)在代码中 对 Java 代码的更改正在传播,即使
简介 我不知道这是一个错误还是我完全遗漏了什么。 我有一个项目(Windows 服务),我们称它为 WINSERV。我有 3 个 DLL,这取决于它们来自 3 个独立的项目(项目 A、B、C)。但是
我是一名优秀的程序员,十分优秀!