Rayrun
← Back to Discord Forum

How To Capture Multiple Elements With Single Locator

Hi there, I want to capture all the elements may be in the list or array found on single locator without using Nth method. Any help would be appreciated.

This thread is trying to answer question "How can multiple elements be captured with a single locator in Playwright using .NET without using the Nth method?"

7 replies

await locator.all()

Thanks @shivaguy, will this work for Playwright using .NET?

https://playwright.dev/dotnet/docs/api/class-locator#locator-all

foreach (var li in await page.GetByRole('listitem').AllAsync())
  await li.ClickAsync();

Sorry, I didn't see the dotnet tag in your question. I am fluent in Playwright for JS, but .NET api is similar and you can find similar methods in the Playwright docs.

The ,NET api has a Locator.AllAsync method similar to the JS api.👍🏻

Might work, but might be flaky depending...? My concern is the note that .AllAsync() will not wait. At best it seems confusing. I've never had an issue doing:

int cnt = await page.locator("...").CountAsync(); for( int idx = 0 ; idx < cnt; idx++) { var item = xxx.Nth(idx); }

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.