最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - WebdriverIO loop through element list - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Feb 1, 2018 at 8:43 Maciej Białorucki 5615 silver badges16 bronze badges asked Feb 1, 2018 at 8:21 Tomas EglinskasTomas Eglinskas 8553 gold badges12 silver badges25 bronze badges 1
  • You can get the idea here StaleElementReference Exception in PageFactory – undetected Selenium Commented Feb 1, 2018 at 15:11
Add a ment  | 

1 Answer 1

Reset to default 4

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!

发布评论

评论列表(0)

  1. 暂无评论