Counting elements with the same selector in webdriver.io

可紊 提交于 2021-01-26 14:51:41

问题


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?


回答1:


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.




回答2:


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

client.elements('.selector').then(function (elems) {
    console.log(elems.value.length);
});



回答3:


Or you can write to a variable and later use it

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


来源:https://stackoverflow.com/questions/23274482/counting-elements-with-the-same-selector-in-webdriver-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!