Javascript 量角器 - 失败:过时的元素参考:元素未附加到页面文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29928856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:20:13  来源:igfitidea点击:

Protractor - Failed: stale element reference: element is not attached to the page document

javascriptangularjsseleniumjasmineprotractor

提问by Daniel Bogart

I have a function in my protractor e2e page object that unchecks several options from a dropdown menu. It had previously worked fine, but now I'm getting the following error:

我的量角器 e2e 页面对象中有一个函数,可以从下拉菜单中取消选中多个选项。它以前运行良好,但现在我收到以下错误:

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

失败:过时的元素引用:元素未附加到页面文档

I have tried fetching the elements on each iteration of the for loop, but the for loop executes before the promise is resolved the first time, meaning that the "limit" value for x is passed repeatedly, and the test just clicks on the same dropdown option several times.

我已经尝试在 for 循环的每次迭代中获取元素,但是 for 循环在第一次解析 promise 之前执行,这意味着 x 的“限制”值被重复传递,并且测试只单击同一个下拉列表多次选择。

this.uncheckColumns = function(limit) {
    element(by.className('fa-cog')).click();
    element.all(by.className('multiSelectLi')).then(function(options) {
        for (x = 1; x < limit; x++) {
            options[x].click();
        };
    });
};

采纳答案by alecxe

How about using each(element, index):

如何使用each(element, index)

element.all(by.className('multiSelectLi')).each(function(option, index) {
    if (index < limit) {
        option.click();
    }
});

Or, in conjunction with filter(element, index):

或者,结合filter(element, index)

element.all(by.className('multiSelectLi')).filter(function(option, index) {
    return index < limit;
}).each(function(option) {
    option.click();
});


Also, a naive approach to solve the problem (calling element.all()continuously in the loop):

此外,一种解决问题的幼稚方法(element.all()在循环中不断调用):

for (var index = 0; index < limit; index++) {
    var option = element.all(by.className('multiSelectLi')).get(index);
    option.click();
};