Using playwright C# I've been trying to work with multiple elements that use the same locator.
Is there any functionality in playwright which is equivalent to the FindElements method from selenium? This method allows us to store multiple elements of the same locator in a collection which can then be used to access individual elements. I actually need to perform actions of each of the elements so having them in a collection where I can access them by index works well for me.
It seems that due to playwrights locator strictness that there is nothing suitable to achieve the same result as Selenium Findelements. I temporarily tried using ElementHandles but this is not recommended and still proves problematic when trying to access elements.
Playwright have First, Last and Nth() locator options which allow us to pick a specific element but I prefer having access to all of the elements in list, accessible by index which I find to be more useful when working with multiple elect's in the same or similar areas.
I ended up writing my own code to store the elements in a list but I wondered what others are using as a substitute for FindElements?
IList<ILocator> elements = new List<ILocator>();
int listSize = await Page.Locator("div #internalSearch span.e-list-text").CountAsync();
for (int j = 0; j < listSize; j++)
{ ILocator locator = Page.Locator("div #internalSearch span.e-list-text").Nth(j);
elements.Add(locator);
}
Assert.True(elements.Count == listSize);