medium

Error: strict mode violation: locator resolved to N elements

A locator matched more than one element, but the action requires exactly one. Playwright's strict mode (enabled by default) prevents ambiguous interactions.

Common Causes

  1. 1CSS selector is too broad (e.g., 'button' matches multiple buttons)
  2. 2Multiple elements share the same test ID or class
  3. 3Dynamic content added duplicate elements to the DOM
  4. 4Component rendered multiple times on the page

How to Fix

Use a more specific selector

// Too broad:
await page.locator('button').click();

// Specific:
await page.locator('button[data-testid="submit"]').click();
// Or use .first(), .nth(), .last():
await page.locator('button').first().click();

Narrow down the selector to match exactly one element, or explicitly pick which one you want.

Use getByRole with name for accessible selectors

await page.getByRole('button', { name: 'Submit' }).click();

Role-based selectors are more resilient and usually more specific than CSS selectors.

How ObserveOne Helps

ObserveOne identifies ambiguous selectors in your test suite and recommends unique, resilient alternatives.

Start Monitoring Free

Related Errors