Rup Rup - 10 months ago 40
Node.js Question

WebdDriverJS/ NodeJS: 'Element is not attached to the page document' Error

I am completely new to WebDriver (chromedriver) and only have super basic knowledge of javascript syntax so I'm sorry if my code looks like spaghetti.

I have a

function that finds all elements in a class and clicks on them.

//Click every element of an array
var clickAllElements = function (el, screenShot){
//Store elements in an array
var findElements = driver.findElements(By.css(el));
findElements.then(function (options) {
//Then iterate through array (elem) {
//Click on each item in the array
return {
//Then get the text content inside each element
//Log the text to the console.
console.log(" - Selecting " + text.trim());
if (screenShot) {

This has worked fine but now I have the following three elements.

enter image description here

And here is the html/css for them as shown in Chrome inspector

enter image description here

I want my
function to click on the x on each value so as to delete them. When I do
clickAllElements('.token > a');
it actually does delete the first one, but after that I get a "stale element reference: element is not attached to the page document." I don't understand the error because those elements are clearly there. How can I get around this? (using a javascripit solution preferably);

Answer Source

There are two possibilities:

The element is deleted immediately after clicking, but is referenced later


In this case you only need to NOT make further references to the object in case of delete links. Add a parameter to this function or, better yet, create another one just for deleting all links.

The element gets updated after clicking

You can also solve this by writing another function, but in this case you can also re-write the function to loop through each element by index, refreshing the list of elements each time after the click and getting the text from the one specified by index.