question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How to identify several elements with the same locator and click on each of them?

See original GitHub issue

I have a list of checkboxes and I need to click on all of them. Depending on some factors, the list count can differ, but the locator will be the same for all checkboxes ('input[aria-label="Select All"]').

How do I iterate over them so I can click each one?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
yury-scommented, Feb 4, 2022

Please use locators instead of element handles as the latter are more error prone. Assuming that the set of check-boxes stays static while you are checking individual items, you can implement it like this:

  // Locator will resolve to all elements matching the selector
  const checkboxes = page.locator('input[aria-label="Select All"]');
  const promises = [];
  // Get current number of elements in the page a click each of them concurrently
  for (let index = 0; index < await (checkboxes.count()); index++)
    promises.push(checkboxes.nth(index).click());
  // Wait for all clicks to finish
  await Promise.all(promises);

If the state of the page may change after clicking you may consider locator.evaluateAll to run some code on all elements in the page.

0reactions
DanielStoica85commented, Feb 5, 2022

@yury-s Should such an example be added to the documentation? I could not find it and I think this is a pretty common use case.

For some strange reason, this doesn’t work for me. Using that locator doesn’t actually find those checkboxes, even though I know for sure that the locator is correct. But if, for example, I do something like const checkbox = page.locator('input[aria-label="Select All"]').first();, it does find it. Very very strange.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are Selenium Locators?How to use them to find web ...
To access all these locators, Selenium provides the “By” class, which helps in locating elements within the DOM. It offers several different ......
Read more >
Locators in Selenium- How To Locate Elements On Web-page?
Locator s are defined as an address that identifies a web element uniquely within the webpage. It is a command that tells Selenium...
Read more >
Locators In Selenium WebDriver With Examples - LambdaTest
This blog deep dives into the multiple locators in Selenium WebDriver and ... Click on it and navigate to the element you wish...
Read more >
How to locate an element where multiple elements have same ...
I have tried to identify the element using selenium IDE, there itself it's not identifying(highlighting) element. – user5167985. Aug 9, 2018 at ...
Read more >
Selenium Webdriver - Identify Multiple Elements - Tutorialspoint
It is not recommended to identify multiple elements by the locator id, since the value of an id attribute is unique to an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found