Protractor 查找并单击元素

标签 protractor

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

StaleElementReferenceError: stale element reference: element is not attached to the page document

有人可以向我解释我做错了什么吗?如果我通过 css 定位它,它工作正常。只有当我使用 Protractor 扩展时它才会失败。页面上没有任何变化,我尝试执行“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;
            }
        });
    });

一个额外的优点是向我解释如何让调试器工作。我正在使用最新版本的 Protractor (0.20.1)

Failed to open socket on port 5858, waiting 1000 ms before retrying

前转储

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

最佳答案

我认为潜在的问题是我在调用 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();
        };
      });
    });
}

* 将下面的内容留在此处以防万一有人想要一个如何执行更复杂/自定义 promise 流程的示例*

我找到了 解决方案,但它的方法太复杂了。希望有人能帮我简化这个..

我认为潜在的问题是我在调用 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()

关于 Protractor 查找并单击元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277738/

相关文章:

selenium - 运行时出现UNABLE_TO_GET_ISSUER_CERT_LOCALLY错误——webdriver-manager通过npm启动

javascript - 向 div 插入值

javascript - 追加到 Jasmine 中的 'IT'描述名

javascript - 遵循页面对象样式指南时找不到元素

javascript - 如何点击 Protractor 中的隐藏元素?

angular - 如何将 Protractor 与 angular-2 表单控件一起使用?

javascript - 是否可以更改 Protractor 事件队列或控制流?

javascript - 使用 Protractor 测试页面上所有链接的有效性

javascript - 使用 Protractor -webdrivers 的 Safari 历史导航中的未知错误

javascript - 如果我们有 5 秒后隐藏的时间间隔,则无法获取 Growl 消息