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();
};
});
};
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();
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With