gpt4 book ai didi

java - 在现有元素上执行 javascript 时,GhostDriver 抛出陈旧异常 "Element does not exist in cache"

转载 作者:行者123 更新时间:2023-12-03 20:25:17 24 4
gpt4 key购买 nike


GhostDriver 有陈旧异常的问题,冒泡上次执行时发生了一些变化。
在这个{Random “Element is no longer attached to the DOM” StaleElementReferenceException } 问题,有超过 16k 的浏览量,有人在讲述竞争条件和测试时发生的变化,但我的代码执行速度如此之快,以至于我无法相信某些事情发生了变化。
我什么都没改变,所有代码都执行得很快,也许页面本身在测试片段执行的小时间范围内发生了变化。
myLibWorks.findElements(..可以并返回元素,它使用 FluentWait<SearchContext>然后该元素在方法返回时可用。
它抛出:

Element does not exist in cache

在我尝试在元素上执行 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
//-------------------------------------------------
}
}
}
}
}

Element does not exist in cache caused by Request => {"headers":{"Accept":"application/json, image/png","Connection":"Keep-Alive","Content-Length":"84","Content-Type":"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"} Command duration or timeout: 736 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '2.32.0', revision: '6c40c18', time: '2013-04-09 17:22:56' System info: os.name: 'Linux', os.arch: 'i386', os.version: '3.8.0-19-generic', java.version: '1.7.0_21' Session ID: efc7cf60-d8f6-11e2-9f07-192e7e451712 Driver info: 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>

```

不要忘记将 selenium 版本替换为您正在使用的版本。

希望对您有所帮助!

关于java - 在现有元素上执行 javascript 时,GhostDriver 抛出陈旧异常 "Element does not exist in cache",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17200888/

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