I want to fetch list of all elements present with a given locator and then perform click on any random element from that list.
The problem I am facing is that I am not able to grab the array of elements with given locator. Can someone please help
There are a number of ways to do this, depending on which part of the element you want to grab. If your elements all have an ID, for example, you can use I.grabAttributeFromAll(locator, 'id')
. This of course will give you an array of IDs that you can then use as locators.
For example:
const id_array = await I.grabAttributeFromAll('.menu_option', 'id')
If that doesn’t work, you could grab another attribute (e.g. ‘name’, ‘class’, etc.), or you could go with I.grabTextFromAll
, I.grabHTMLFromAll
or I.grabValueFromAll
. From there, it’s not hard to build a new Xpath locator to use for your random clicks.