I got this working in the end using the evaluateAll method. Example code:

async waitForAllHidden(locator: Locator, timeout: number = 10000) {
    const start = Date.now()
    const elementsVisible = async () => (
        await locator.evaluateAll(elements =>
            elements.map(element => element.hidden))
    ).includes(false)

    while (await elementsVisible()) {
        if (start + timeout < Date.now()) {
            throw (`Timeout waiting for all elements to be hidden.
                    Locator: ${locator}. Timeout: ${timeout}ms`);
        }
    }
    console.log(`All elements hidden: ${locator}`)
}

Update for 2023:

There are new methods available which can now be used for this purpose:

toHaveCount() e.g.

await expect(yourLocator).toHaveCount(0);

poll() e.g.

await expect.poll(async () => {
  for (const e of await yourLocator.all()) {
    if (await e.isVisible()) return false
  }
  return true
}, {
  message: 'youLocator is still visible', // optional custom error message
  intervals: [1_000, 2_000, 10_000], // optional polling overrides
  timeout: 10000, // optional timeout override
}).toBe(true);
Answer from bee-anchor on Stack Overflow
🌐
Playwright
playwright.dev › locators
Locators | Playwright
This means that all operations on locators that imply some target DOM element will throw an exception if more than one element matches. For example, the following call throws if there are several buttons in the DOM: ... On the other hand, Playwright ...
🌐
Playwright
playwright.dev › locator
Locator | Playwright
Resolves given locator to the first matching DOM element. If there are no matching elements, waits for one. If multiple elements match the locator, throws.
Discussions

Anyone annoyed by locator strictness in playwright?
Iterate using `.all()`? https://playwright.dev/python/docs/locators#rare-use-cases More on reddit.com
🌐 r/Playwright
3
0
November 21, 2023
[Question] Is there way to iterate the Locator elements?
Hi all, I would like to iterate the Locator elements while using the 'of' operator. For example, if I found several same elements with a page.locator function - is there way to check each o... More on github.com
🌐 github.com
24
December 1, 2021
How do I use the all() method to iterate over multiple elements in Playwright?
In Playwright, you can use the all() method on Locator instances to iterate over multiple elements that match a certain selector. More on ray.run
🌐 ray.run
1
0
How to deal with multiple elements found error?
You could try using wait_for() on the locator at the end of the function, which can check if the element is visible. If the element you want will always be visible, then loop through .all() to find the visible one, or pythons filter: target = list(filter(lambda l: l.is_visible(), elements.all()).pop() Python's filter() is useful for checking locators by their attributes that Playwright doesn't support: target = list(filter(lambda l: "my_class" in str(l.get_attribute("class")), elements.all())).pop() You could also try the locator.or_() method: target = elements.first().or_(elements.nth(1)) I also like to narrow down duplicate locators by their parent elements. Find a container div that's visible and easy to identify, then search for your locator under that. But that may not be helpful in this situation. I hope some of this helps. More on reddit.com
🌐 r/Playwright
10
2
August 6, 2024
Top answer
1 of 3
7

I got this working in the end using the evaluateAll method. Example code:

async waitForAllHidden(locator: Locator, timeout: number = 10000) {
    const start = Date.now()
    const elementsVisible = async () => (
        await locator.evaluateAll(elements =>
            elements.map(element => element.hidden))
    ).includes(false)

    while (await elementsVisible()) {
        if (start + timeout < Date.now()) {
            throw (`Timeout waiting for all elements to be hidden.
                    Locator: ${locator}. Timeout: ${timeout}ms`);
        }
    }
    console.log(`All elements hidden: ${locator}`)
}

Update for 2023:

There are new methods available which can now be used for this purpose:

toHaveCount() e.g.

await expect(yourLocator).toHaveCount(0);

poll() e.g.

await expect.poll(async () => {
  for (const e of await yourLocator.all()) {
    if (await e.isVisible()) return false
  }
  return true
}, {
  message: 'youLocator is still visible', // optional custom error message
  intervals: [1_000, 2_000, 10_000], // optional polling overrides
  timeout: 10000, // optional timeout override
}).toBe(true);
2 of 3
4

Spinner or an "..loading" text element's presence cannot be verified for sure as it may or may not appear during page load depending on page performance .

However , it's eventual disappearance can be verified.

Use toHaveCount() (Added in: v1.20)

await expect(locator).toHaveCount(0,{timeout:5000});

Timeout: To avoid infinite loops timeout can be configured globally or passed as an parameter in the function call.

// playwright.config.ts
import { defineConfig } from '@playwright/test';

export default defineConfig({
  expect: {
    timeout: 5000
  },
});

Tip: Its better to keep local timeouts lower than global timeout to avoid any race conditions if both exist.

Reference: https://github.com/microsoft/playwright/issues/11988

https://stackoverflow.com/a/74209034/1831456

How can I assert that an element is NOT on the page in playwright?

🌐
Reddit
reddit.com › r/playwright › anyone annoyed by locator strictness in playwright?
r/Playwright on Reddit: Anyone annoyed by locator strictness in playwright?
November 21, 2023 -

I came from selenium where i would constantly use findelements then foreach thru the results to assert against many elements in a loop rather than doing 1 at a time. I realize that you can get multiple elements with a locator in playwright but the result isn’t enumerable so i have to jump thru more hoops to loop over multiple elements. Im also not sure how to disable strictness, but pretty sure it wouldn’t makw a difference. Am i missing something?

Top answer
1 of 2
5
Iterate using `.all()`? https://playwright.dev/python/docs/locators#rare-use-cases
2 of 2
2
You should be able to iterate through a collection of Locators. Assuming you're using Javascript, I asked AI and it said this: In Playwright, you can iterate over multiple elements with the same selector using several methods. The most common methods include using the elementHandles() method, the all() method, or a combination of the count() and nth() methods. Using elementHandles() method: The elementHandles() method returns an array of element handles for each matching element. You can then iterate over this array and perform actions on each element. Here's an example: for (const el of await checkboxLocator.elementHandles()) { await el.check(); } In this example, we're locating all checkboxes within table rows and checking each one 1. Using all() method: The all() method on Locator instances is used to iterate over multiple elements that match a certain selector. It returns an array of locators, each pointing to one of the matching elements. Here's an example: await page.waitForSelector('your-selector'); const locators = page.locator('your-selector').all(); for (const locator of locators) { await locator.click(); } In this example, we're waiting for all elements matching 'your-selector' to load, then clicking each one 3. Using count() and nth() methods: The count() method returns the number of elements matching the locator, and the nth() method returns the nth element matching the locator. You can use these methods in combination to iterate over each element. Here's an example: const items = page.locator('ul > li'); for (let i = 0; i < await items.count(); i++) { await items.nth(i).click(); } In this example, we're locating all list items within an unordered list and clicking each one 1. Remember to handle dynamic content properly and wait for relevant content before calling all(), elementHandles(), count(), or nth(). This can be done using Playwright's auto-waiting mechanism or manually waiting for specific events or conditions.
🌐
Playwright
playwright.dev › other locators
Other locators | Playwright
We recommend prioritizing user-visible locators instead. Sometimes, it is hard to come up with a good selector to the target element when it lacks distinctive features. In this case, using Playwright layout CSS pseudo-classes could help. These can be combined with regular CSS to pinpoint one of the multiple ...
🌐
BugBug
bugbug.io › blog › testing frameworks
Playwright Locators - Comprehensive Guide
November 7, 2025 - Locators can match multiple elements, filter by text, and even find elements that contain or exclude specific text, making them a powerful tool for building stable and maintainable automated tests.
🌐
GitHub
github.com › microsoft › playwright › issues › 10648
[Question] Is there way to iterate the Locator elements? · Issue #10648 · microsoft/playwright
December 1, 2021 - let arrayOfLocators = page.locator('element); for (let singleLocator of arrayOfLocators) { const textOfElement = await singleLocator.innerText(); }
Author   AlexKomanov
Find elsewhere
🌐
Playwright
playwright.dev › locator
Locator | Playwright Python
It's zero based, nth(0) selects the first element. ... Creates a locator matching all elements that match one or both of the two locators. Note that when both locators match something, the resulting locator will have multiple ...
🌐
Testomat
testomat.io › home › playwright locators. handle elements: inputs, buttons, dropdown, frames, etc.
Playwright Locators Guide | Elements, Inputs, Buttons & Frames
February 2, 2026 - The first involves searching for an element by specifying the locator with the await page and then taking some action with the multiple elements. The second involves directly invoking the action.
Address   Ul. Koszykarska 27b-26, Kraków
🌐
Better Stack
betterstack.com › community › questions › playwright-multiple-elements
How to Perform Actions on Multiple Elements in Playwright | Better Stack Community
February 27, 2024 - Playwright · Better Stack Team · Updated on February 27, 2024 · In most cases, you'll be writing tests that performs an action on one element at a time. However, if you need to perform an action on multiple elements at once (such as clicking on several items in a list), you can do it as follows: Copied! test('user can click multiple list items', async ({ page }) => { const items = page.locator('ul > li'); for (let i = 0; i < await items.count(); i++) { await items.nth(i).click(); } }) Once you've selected all the items using the appropriate locator method, you can iterate over the found items using and then perform the relevant action in the body of the loop.
🌐
Playwright
playwright.dev › locator
Locator | Playwright .NET
It's zero based, nth(0) selects the first element. ... Creates a locator matching all elements that match one or both of the two locators. Note that when both locators match something, the resulting locator will have multiple ...
🌐
Reddit
reddit.com › r/playwright › how to deal with multiple elements found error?
r/Playwright on Reddit: How to deal with multiple elements found error?
August 6, 2024 -

I am switching from Selenium to Playwright and I am creating python behave POM framework.
MY problem is that the page tested has multiple elements (doing exactly the same) with absolutely nothing unique and devs refuse to help me. So I need to collect all found elements and say to use one of them. Here is the function in base class:

def find_element(self, locator):
    elements = self.context.page.locator(locator).all()
    if elements:
        if len(elements) == 1:
            return elements[0]
        else:
            return elements[1]
    else:
        raise Exception(f'No elements found with locator {locator}!')

However, using this function in page classes, does not benefit from Playwright auto waiting for elements and my tests fail with the exception, unless I manually put sleeps in the code.
Is there any way I can keep the workaround for dealing with no unique elements but still benefit from auto waiting?

🌐
Playwright
playwright.dev › locator
Locator | Playwright Java
It's zero based, nth(0) selects the first element. ... Creates a locator matching all elements that match one or both of the two locators. Note that when both locators match something, the resulting locator will have multiple ...
🌐
Testmu
testmu.ai › testmu ai › blog › how to use playwright locators: a detailed guide | testmu ai
How to Use Playwright Locators: A Detailed Guide | TestMu AI (Formerly LambdaTest)
January 13, 2026 - Filtering is used to narrow down ... to find an element containing a specific text piece. Chaining is used to combine multiple locators in a single search....
🌐
Playwright
playwright.dev › locatorassertions
LocatorAssertions | Playwright
Ensures the Locator points to an element with the given input value. You can use regular expressions for the value as well. ... Expected value. ... Time to retry the assertion for in milliseconds. Defaults to timeout in TestConfig.expect. ... Ensures the Locator points to multi-select/combobox (i.e. a select with the multiple attribute) and the specified values are selected.
🌐
HexDocs
hexdocs.pm › playwright › Playwright.Locator.html
Playwright.Locator — playwright v1.49.1-alpha.2
Resolves the given Playwright.Locator to the first matching DOM element. If no elements matching the query are visible, waits for them up to a given timeout. If multiple elements match the selector, throws.
🌐
GitHub
github.com › microsoft › playwright › issues › 10726
[Feature] Allow to work with multiple elements that match the same locator · Issue #10726 · microsoft/playwright
December 6, 2021 - Use case 1: elements are in the DOM but can be hidden or shown after some action and I'd like to wait until all of them hidden / visible · Use case 2: there is some table or grid with headers, each header has title and I'd like to save all titles in array. For now I can do it like this: const headers: Locator = page.locator(`some locator`); const headerTitles: string[] = await headers.evaluateAll((els: HTMLElement[]) => els.map((el: HTMLElement) => el.innerHTML));
Author   RockMinsk
🌐
TestGrid
testgrid.io › test automation › playwright selectors and locators: everything you need to know
Playwright Selectors and Locators: Everything You Need to Know
October 8, 2025 - They provide a higher-level and more expressive way to locate elements compared to traditional selector types like CSS selectors and XPath. Playwright comes with multiple types of locators for identifying elements and fields on the web page.
🌐
Oxylabs
oxylabs.io › home › resources › web scraping faq › playwright › locators class
How to Locate Element by Class in Playwright?
When dealing with multiple elements of the same class, consider using `page.locator().element_handles()` to handle each element individually for actions like text extraction or attribute checks.