量角器查找和单击元素

时间:2014-03-09 02:37:04

标签: protractor

我的设置非常简单。我需要能够找到一个链接并单击它。但每当我找到它并click()我得到

  

StaleElementReferenceError:陈旧元素引用:元素未附加到页面文档

有人可以向我解释我做错了什么吗?如果我通过CSS定位它,它工作正常。它只有当我使用量角器扩展它失败时。页面上没有任何变化,我尝试过“waitForAngular”以及长时间超时。结果相同。

<li ng-click="getLocation(l)" class="" ng-repeat="l in location"  >
   <div class="location_name">{{ l.LocationID }} - {{l.LocationName}}</div>
</li>

var locations= element.all(
    by.binding('{{l.LocationName}}'))
    .each(function(item){
        item.getText().then(function(text){
            if(text == name){
                console.log("found", item);
                item.click()
                console.log("clicked")
                found = true;
            }
        });
    });

另外一个好处是向我解释如何使调试器工作。我正在使用最新版本的量角器(0.20.1)

  

无法在端口5858上打开套接字,在重试前等待1000毫秒

Ex Dump

StaleElementReferenceError: stale element reference: element is not attached to the page document
  (Session info: chrome=33.0.1750.146)
  (Driver info: chromedriver=2.9.248307,platform=Mac OS X 10.9.1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 5 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:54:28'
System info: host: '.home', ip: '192.168.1.2', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.1', java.version: '1.6.0_65'
Session ID: 935f7630f50d9b1b4c8da73261e54153
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=/var/folders/qj/xvhg6hv5245cryry73wygbkw0000gn/T/.org.chromium.Chromium.b5z7Xe}, rotatable=false, locationContextEnabled=true, version=33.0.1750.146, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}]
   Stacktrace:
     Error
    at null.<anonymous> (/Users//work////selenium/spec/locationsSpec.js:21:3)
    at Object.<anonymous> (/Users//work////selenium/spec/locationsSpec.js:7:1)
At async task:
      StaleElementReferenceError: stale element reference: element is not attached to the page document
  (Session info: chrome=33.0.1750.146)
  (Driver info: chromedriver=2.9.248307,platform=Mac OS X 10.9.1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 5 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.40.0', revision: 'fbe29a9', time: '2014-02-19 20:54:28'
System info: host: '.home', ip: '192.168.1.2', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.1', java.version: '1.6.0_65'
Session ID: 935f7630f50d9b1b4c8da73261e54153
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=/var/folders/qj/xvhg6hv5245cryry73wygbkw0000gn/T/.org.chromium.Chromium.b5z7Xe}, rotatable=false, locationContextEnabled=true, version=33.0.1750.146, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}]
==== async task ====
WebElement.getText()
    at element.(anonymous function) [as getText] (/Users//work////selenium/node_modules/protractor/lib/protractor.js:599:32)
    at /Users//work////selenium/helpers/locations.js:10:18
    at /Users//work////selenium/node_modules/protractor/lib/protractor.js:367:11
    at Array.forEach (native)
    at /Users//work////selenium/node_modules/protractor/lib/protractor.js:366:13
==== async task ====
asynchronous test function

2 个答案:

答案 0 :(得分:3)

我认为潜在的问题是我在调用getText时正在导航。下面的代码块完成了我需要的东西

LocationsPage.prototype.goTo = function(name) {
    var locations  = element.all(by.repeater('l in location'));
    expect(locations.count()).toNotBe(0);
    return locations.map(
            function(locationElement, index) {
              return {
                index: index,
                text: locationElement.getText()
              };
            }
    ).then(function(items) {
      _.each(items, function(item) {
        if (item.text == name) {
            console.log("clicking ", name)
            locations.get(item.index).click();
        };
      });
    });
}

*请留下以下内容,以防有人想要一个如何进行更复杂/自定义承诺流程的示例*

我发现一个解决方案,但它的方式很复杂。希望有人可以帮我简化这个..

我认为潜在的问题是我在调用getText时正在离开。

LocationsPage.prototype.goTo = function(name){
    var locations  = element.all(by.repeater('l in location'));
    expect(locations.count()).toNotBe(0);
    return protractor.promise.createFlow(function(flow){
        var found;
        var locations  = element.all(by.repeater('l in location'));
        var searchPromise = protractor.promise.defer();
        flow.execute(function(){
            var idx = 0, found = -1;
            locations.each(function(item){
                var _idx = idx;
                if(found >= 0){
                    console.log("skipping")
                    return;
                }
                item.getText().then(function(text){
                    if(text == name){
                        console.log("setting found.")
                        found = _idx;
                    }
                    if(found >= 0 || _idx === locations.count()-1){
                         searchPromise.fulfill(found);
                    }
                });
                idx++;
            });
            return searchPromise.promise;
        })
        flow.execute(function(){
            var promise = protractor.promise.defer();
            searchPromise.then(function(idx){
                locations.get(idx).click().then(function(){
                    promise.fulfill(true);
                });

            })

            return promise.promise;
        });
    });

}

你会注意到其中的一些其他内容protractor.promise.createFlow我必须使用它,因为当我expects使用protractor.promise.defer()

时我的{{1}}无法正确解析

答案 1 :(得分:0)

问题是ng-repeat。每当更新发生时,对象都将自行替换。我实际上建议更新他们ng-repeat到ng-repeat="l in location track by $index",以便正确替换它们。这可能无法解决问题。

而不是使用.each执行.filter

var locations = element.all(by.binding('{{l.LocationName}}'))
    .filter(function(elem, index) {
       return elem.getText().then(function(text) {return text === name;});
    }).click();

http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.filter