1

I want to learn webdriverio. I try to run this code:

client.init().
url('https://www.example.com').
elements('p').then((result) => {
    for (i = 0; i < result.value.length; i++) {
        (client.elementIdText(result.value[i])).
        then((re) => console.log(re))
    }
})

but that logs out nothing.

I know i can do it using getText('p'), but just wanna know how to do it using elements('p').

Talha Awan
  • 4,573
  • 4
  • 25
  • 40
Genady Mager
  • 61
  • 1
  • 7
  • `elements('p').value.forEach((element) => { return console.log(browser.elementIdText(element.ELEMENT).value)})` Will do the trick. – tehbeardedone Aug 02 '17 at 15:38

1 Answers1

3

Hope this hint will help you in finding your answer:

let totalElements = $$('p').map((result) => {
    return result.getText();
});
console.log(totalElements);

Or this option

$$('p').forEach(function(result){
    console.log(result.getText());
});

Note: $$ Link

And to get it done from your code please do the same $$, remove .value and change the method to getText(). As there is nothing returned because elementIdText() will take only selector ID as an argument. And <p> is not an ID. Refer here for elmentIdText()

for(i=0; i<result.length; i++){
  (client.getText(result[i])).
   then((re) => console.log(re))
}
T Gurung
  • 333
  • 1
  • 7
  • Wouldn't it be `elements('p').value.map((result) => { return result.getText(); });`? Otherwise you will get `TypeError: browser.elements(...).map is not a function`. – tehbeardedone Aug 02 '17 at 15:09
  • Thanks for spotting it. With regards to code that was only a hint to iterate. As the `elements` will return an array of objects and then need to do the `.value` to retrieve that. So replaced the `elements` with `$$` which just returns a normal array – T Gurung Aug 03 '17 at 11:11