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

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

我的量角器 e2e 页面对象中有一个函数可以取消选中下拉菜单中的几个选项。它以前工作正常,但现在我收到以下错误:

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

我已经尝试在 for 循环的每次迭代中获取元素,但是 for 循环在第一次解决承诺之前执行,这意味着 x 的 "limit" 值被重复传递,并且测试只是多次点击同一个下拉选项。

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();
        };
    });
};

如何使用each(element, index):

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

或者,与filter(element, index)结合使用:

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

另外,一个天真的解决问题的方法(在循环中不断调用element.all()):

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