Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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.

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();
        };
    });
};
like image 454
Daniel Bogart Avatar asked Apr 28 '15 20:04

Daniel Bogart


1 Answers

How about using each(element, index):

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

Or, in conjunction with 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):

for (var index = 0; index < limit; index++) {
    var option = element.all(by.className('multiSelectLi')).get(index);
    option.click();
};
like image 194
alecxe Avatar answered Sep 21 '22 11:09

alecxe