Output List of Webelements in chronological order with nodejs/selenium?

Viewed 134

This is my code, i am trying to fetch all elements with specific class, but whatever i tried, it gets output in the wrong order, i want to start from the first element to last, top to bottom.

    let tree = await driver.findElements(By.css("._26l3y"));
    Promise.all(tree).then(function(values) {
        tree.forEach(function (tree, index) {
            tree.getText().then(function (x) {
                if(x == "5"){
                  console.log(`GOLD  ${index} //`);
                }else{
                  console.log(`${x}  ${index} //`);
                }
                // if(x == ''){
                //   console.log("empt");
                // }
            });
        });
      });

first is the text of the element, the second is the index from the forEach function. i do not know how to go through Webelements natively yet, so this was my solution, i would then validate if the text, matched with the order on the webpage

My console

GOLD  0 //
GOLD  1 //
GOLD  2 //
GOLD  3 //
GOLD  5 //
GOLD  6 //
GOLD  4 //
GOLD  7 //
2  8 //
3  9 //
GOLD  11 //
1  10 //
2  12 //
1  15 //
GOLD  13 //
1  14 //
1  18 //
1  43 //
  55 //
  61 //
  64 //
  66 //
  56 //
  65 //
1  44 //
  57 //
  58 //
  62 //
GOLD  16 //
GOLD  17 //
  63 //
  59 //
1  19 //
  50 //
  60 //
  45 //
1  46 //
  48 //
1  20 //
  49 //
1  21 //
1  27 //
1  28 //
1  29 //
1  30 //
1  22 //
1  25 //
1  26 //
GOLD  32 //
  51 //
1  31 //
  47 //
1  33 //
1  34 //
1  35 //
1  38 //
1  39 //
1  41 //
GOLD  23 //
1  40 //
1  36 //
1  24 //
1  37 //
1  42 //
  52 //
  54 //
  53 //
1 Answers

Please try this code.

 this.fetchAllElements = async function(xpath) { 

    let trees= await this.driver.findElements(By.css("._2613"));
    var i=0;            
    for (let tree of trees) {
      text = await tree.getText();
      console.log(text);
      if(text == "5"){
       console.log(`GOLD  ${i} //`);
      }else{
       console.log(`${text}  ${i} //`);
      }
      i++;
      }

  };
Related