Protractor - Failed: stale element reference: element is not attached to the page document
28,740
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();
};
Comments
-
Daniel Bogart almost 2 years
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(); }; }); };
-
Daniel Bogart about 9 yearsSame errors for both -
Failed: stale element reference: element is not attached to the page document
- it seems like because the DOM is changing, I'm required to once again fetch all the 'multiSelectLi' elements. -
alecxe about 9 years@DanielBogart sounds like yes. Well, the naive approach would be to have a
while index < limit
loop maintaining the index manually in the loop, repeatingelement.all
calls on every iteration and useget(index)
to get the n-th element. Let me know if you need an example. -
alecxe about 9 years@DanielBogart provided, please check if it helps or not.