Tomas Eglinskas
Tomas Eglinskas

Reputation: 855

WebdriverIO loop through element list

So I have a table of 250 of rows, and I want to just get all the values from one column and check if they meet the required criteria:

const rows = browser.elements(selector..);

  const numbers = [];
  rows.value.forEach(cellData => {
    const value = browser.elementIdText(cellData.value.ELEMENT).value;

    // some logic to check if the value is ok

    numbers.push(value);
  });
  // check if all numbers are sorted correctly

, but it most of the time it fails on the line (it says stale element reference: element is not attached to the page document):

const value = browser.elementIdText(cellData.value.ELEMENT).value;

I tried doing cellDate.getText(), but there was a Java socket error, could someone help? I assume the selector is not attached to the page as indicated, but I can't figure my head out how to just loop through them all.

Upvotes: 4

Views: 9699

Answers (1)

Denzik
Denzik

Reputation: 316

I had a solution similar to your method before and while it seems to work, I think there might just be some slight adjustments to your code to get what you want. I never had much luck chaining from the end of the elementIdText call.

Step 1: Grab all the Data (browser.elements or browser.$$):

let cellData = browser.$$('selector that matches desired Column Data') 

The above returns an array of JSON WebElements. And as you know you can correctly loop through the array looking at the "values". If you use the selector that matches the Column Values you're looking for you should have all similar data stored in the element.value.ELEMENT.

Step 2: Loop through the cellData array and pluck out the text values of the ELEMENT using browser.elementIdText()

cellData.forEach((elem) => {

  let number = browser.elementIdText(elem.value.ELEMENT)
    //elementIdText also returns a JSON WebElement so it's number.value
    if(number.value === <condition>) {
      console.log('number looks good')
      //perform other on value logic
    }

  })
  //perform other logic still in loop EX: array.push()
})

I hope this helps! Let me know if you hit any snags!

Upvotes: 4

Related Questions