在现有元素上执行javascript时,GhostDriver抛出过时的异常“元素在缓存中不存在”

时间:2013-06-19 20:39:55

标签: java javascript selenium wait ghostdriver


有过时异常的问题,GhostDriver冒泡了一些东西,上次执行时更改了sice 在{Random “Element is no longer attached to the 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  
                    //-------------------------------------------------  
                }  
            }  
        }  
    }  
}  

  

由Request =&gt;引起的缓存中不存在元素   { “头”:{ “接受”:“应用/ JSON,   图像/ PNG“‘连接’:‘保持活动’,‘内容长度’:‘84’,‘内容类型’:”应用/ JSON;   字符集= UTF-8" , “主机”: “127.0.0.1:4444”}, “httpVersion”: “1.1”, “方法”: “POST”, “后”: “{\” ARGS \“:[{ \ “元素\”:\ “:WDC:1371656598440 \”}],\ “脚本\”:\“返回   参数[0] .innerHTML \ “}”, “URL”: “/执行”, “urlParsed”:{ “锚”: “”, “查询”: “”, “文件”: “执行”, “目录” : “/”, “路径”: “/执行”, “相对的”: “/执行”, “端口”: “”, “宿主”: “”, “密码”: “”, “用户”: “” “用户信息”: “”, “权威”: “”, “协议”: “”, “源”: “/执行”, “queryKey”:{}, “块”:[ “执行”]}“, urlOriginal “:”/会话/ efc7cf60-d8f6-11e2-9f07-192e7e451712 /执行“}   命令持续时间或超时:736毫秒有关文档   这个错误,请访问:   http://seleniumhq.org/exceptions/stale_element_reference.html建立   info:version:'2.32.0',修订版:'6c40c18',时间:'2013-04-09   17:22:56'系统信息:os.name:'Linux',os.arch:'i386',os.version:   '3.8.0-19-generic',java.version:'1.7.0_21'会话ID:   efc7cf60-d8f6-11e2-9f07-192e7e451712驱动信息:   org.openqa.selenium.remote.RemoteWebDriver功能   [{platform = LINUX,acceptSslCerts = false,javascriptEnabled = true,   browserName = phantomjs,rotate = 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');
};

1 个答案:

答案 0 :(得分:0)

在使用GhostDriver依赖项和使用Chrome驱动程序运行测试时,我遇到了同样的问题。将这两个addtional依赖项添加到pom.xml解决了这个问题:

```

fh.seek(0)

```

别忘了用你正在使用的版本替换硒版本。

希望它有所帮助!