Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
173
rated 0 times [  176] [ 3]  / answers: 1 / hits: 29548  / 9 Years ago, tue, april 28, 2015, 12:00:00

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

More From » angularjs

 Answers
6

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

[#66843] Monday, April 27, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ryanulyssesb

Total Points: 91
Total Questions: 105
Total Answers: 102

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
;