GhostDriver 在现有元素上执行 javascript 时引发陈旧异常“元素在缓存中不存在”
GhostDriver throwing stale exception "Element does not exist in cache" when executing javascript on existing element
遇到过时异常的问题,GhostDriver 正在冒泡,上次执行时更改了一些东西。
在这个 {随机"元素不再附加到 DOM"StaleElementReferenceException} 问题上,有超过 16k 次浏览,有人在讲述竞争条件和测试时发生变化的事情,但我的代码执行得如此之快,以至于我不敢相信有什么东西发生了变化。
我什么也没改变,所有代码执行速度都很快,也许页面本身在测试片段执行的小时间范围内正在发生变化。myLibWorks.findElements(..
正常并返回元素,它的使用FluentWait<SearchContext>
然后元素在方法返回时可用。
它抛出:
元素在缓存中不存在
当我尝试在元素上执行 JavaScript 之后。
这是我的 Java 代码的简化片段:
by = getBy_A001();
List<WebElement> welCollecN1 = myLibWorks.findElements(driver, timeOutInSeconds, pollingForSecond, by);
if (welCollecN1 != null) {
WebElement wel01 = welCollecN1.iterator().next();
if(wel01 != null)
{
by = getBy_A002();
List<WebElement> welCollecN2 = myLibWorks.findElements(wel01, timeOutInSeconds, pollingForSecond, by);
if (welCollecN2 != null) {
WebElement wel02 = welCollecN2.iterator().next();
if(wel02 != null)
{
String value = null;
value = elm.getText();
if(value.length() == 0) {
//-------------------------------------------------
// REACH here then i think its ok above, this works almost of time too
// THIS line throws "Element does not exist in cache"
value = (String) ((JavascriptExecutor) driver).executeScript(driver, "return arguments[0].innerHTML", wel02); // <<== ERROR
//-------------------------------------------------
}
}
}
}
}
元素在缓存中不存在,由请求 => {"headers":{"Accept":"application/json, image/png","连接":"保持活动","内容长度":"84","内容类型":"application/json; charset=utf-8","Host":"127.0.0.1:4444"},"httpVersion":"1.1","method":"POST","post":"{''"args''":[{''"ELEMENT''":''":wdc:1371656598440''"}],''"script''":''"return arguments[0].innerHTML''"}","url":"/execute","urlParsed":{"anchor":","query":","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":","host":","password":","user":":"userInfo":","authority":","protocol":","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/efc7cf60-d8f6-11e2-9f07-192e7e451712/execute"} 命令持续时间或超时:736 毫秒 有关文档 此错误,请访问: http://seleniumhq.org/exceptions/stale_element_reference.html 构建 信息: 版本: '2.32.0', 修订: '6C40C18', 时间: '2013-04-09 17:22:56' 系统信息: os.name: 'Linux', os.arch: 'i386', os.version: "3.8.0-19-通用",java.version:"1.7.0_21"会话 ID: efc7cf60-d8f6-11e2-9f07-192e7e451712 驱动程序信息: org.openqa.selenium.remote.RemoteWebDriver capabilities [{platform=LINUX, acceptSslCerts=false, javascriptEnabled=true, browserName=phantomjs, rotatable=false, driverVersion=1.0.3, locationContextEnabled=false, version=1.9.0, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=false, browserConnectionEnabled=false, proxy={proxyType=direct}, nativeEvents=true, webStorageEnabled=false, driverName=ghostdriver, applicationCacheEnabled=false, takesScreenshot=true}]} =======
错误看起来从这里冒泡:https://code.google.com/p/phantomjs/source/browse/src/ghostdriver/third_party/webdriver-atoms/deps.js?r=78d90641df12d10b1f30b2bb4c08b92d6aff5f9b
/**
* Retrieves an element from the cache. Will verify that the element is
* still attached to the DOM before returning.
* @param {string} key The element's key in the cache.
* @param {Document=} opt_doc The document whose cache to retrieve the element
* from. Defaults to the current document.
* @return {Element|Window} The cached element.
*/
bot.inject.cache.getElement = function(key, opt_doc) {
key = decodeURIComponent(key);
var doc = opt_doc || document;
var cache = bot.inject.cache.getCache_(doc);
if (!goog.object.containsKey(cache, key)) {
// Throw STALE_ELEMENT_REFERENCE instead of NO_SUCH_ELEMENT since the
// key may have been defined by a prior document's cache.
throw new bot.Error(bot.ErrorCode.STALE_ELEMENT_REFERENCE,
'Element does not exist in cache');
}
var el = cache[key];
// If this is a Window check if it's closed
if (goog.object.containsKey(el, 'setInterval')) {
if (el.closed) {
delete cache[key];
throw new bot.Error(bot.ErrorCode.NO_SUCH_WINDOW,
'Window has been closed.');
}
return el;
}
// Make sure the element is still attached to the DOM before returning.
var node = el;
while (node) {
if (node == doc.documentElement) {
return el;
}
node = node.parentNode;
}
delete cache[key];
throw new bot.Error(bot.ErrorCode.STALE_ELEMENT_REFERENCE,
'Element is no longer attached to the DOM');
};
我在包含 GhostDriver 依赖项并使用 Chrome 驱动程序运行测试时遇到了同样的问题。将这两个额外的依赖项添加到 pom 中.xml解决了这个问题:
'''
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.53.0</version>
</dependency>
'''
不要忘记用您正在使用的版本替换硒版本。
希望对您有所帮助!
- CKEditor Widget-阻止编辑可编辑元素本身
- 如何设置html元素填充的动画
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 使用clickToggle并在单击另一个元素时关闭元素
- 单击时将焦点更改为元素
- 表追加而不附加最后一个元素
- 如何在jQuery中获取元素的形式
- 我可以获得相对于被点击元素的确切点击位置吗
- 在函数中添加数组元素的数值
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 将视口底部滚动到元素底部
- 通过parentNode.removeChild删除元素会引发DOM异常8
- 在属性名称中搜索带有冒号的 xpath 表达式会引发异常(节点.js元素树模块)
- 未捕获的异常:每个数据元素都必须实现一个唯一的“id”属性slick.dataview.js
- 在元素上使用css属性content来异常存储javascript的信息
- 对td元素应用填充引发异常
- GhostDriver 在现有元素上执行 javascript 时引发陈旧异常“元素在缓存中不存在”
- 聚合物芯-ajax在新的聚合物元素中表现异常
- 如何处理量角器中未找到元素的异常