3

由于存在过时异常的问题,GhostDriver 正在冒泡某些内容在上次执行后发生了更改。
在这个 { Random “Element is no longer attach 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 => {"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\":\"返回参数[0].innerHTML\"}"," url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute “,“相对的”:”/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol": "","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/efc7cf60-d8f6-11e2-9f07-192e7e451712/execute"命令持续时间或超时:736 毫秒有关此错误的文档,请访问:chunks":["execute"]},"urlOriginal":"/session/efc7cf60-d8f6-11e2-9f07-192e7e451712/execute"} 命令持续时间或超时:736 毫秒有关此错误的文档,请访问: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-generic', 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,TakeScreenshot=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');
};

4

1 回答 1

0

在包含 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 版本。

希望能帮助到你!

于 2016-11-27T14:22:19.090 回答