6

I am using webdriver.io with chai and mocha for testing.

In one of my tests I need to count how many elements with the same CSS class are in the page. None of the webdriver.io API seems to return an array.

How can it be achieved?

syymza
  • 679
  • 1
  • 8
  • 23
  • This link should answer your question: http://stackoverflow.com/questions/18677516/webdriverjs-finding-multiple-elements – Richard Apr 24 '14 at 16:25
  • 2
    @Richard: well, that link is unfortunately about webdriverjs, not webdriver.io. For reference: https://github.com/camme/webdriverjs#disclaimer – syymza Apr 24 '14 at 16:32

4 Answers4

12

This is how you do it:

client.elements('.myElements', function(err,res) {
    console.log('element count: ',res.value.length);
});

Explanation: with elements you fetch all elements according given selector. It returns an array of webdriver elements which represents the amount of existing elements on the page.

ChristianB
  • 1,967
  • 14
  • 25
  • 1
    Thanks, this does indeed work. Is there a reason for which the .elements() method is not documented currently on the website (I indeed found a mention to it on Github)? – syymza Apr 25 '14 at 08:51
9

For version 4 of webdriver.io, this is the way

client.elements('.selector').then(function (elems) {
    console.log(elems.value.length);
});
isHristov
  • 1,546
  • 2
  • 16
  • 18
4

For version 7.13.2 of webdriver.io, you can try this

 const count = await $$('selector').length

Vaibhav Jagdale
  • 229
  • 2
  • 11
1

Or you can write to a variable and later use it

let smth = browser.elements('selector').value.length;