# Playwright strict mode violation

Classification: selector-drift
Frameworks: Playwright
Canonical: https://automationtester.in/failures/playwright-strict-mode-violation

## Recognizable error

```text
Error: strict mode violation: locator('button') resolved to 2 elements
```

## What it means

A Playwright locator expected to identify one element matched more than one, so Playwright refused an ambiguous action.

## Ranked causes, checks, and fixes

### 1. The accessible name is duplicated

Two controls now share the role and name used by the locator.

Diagnostic: Inspect every match and confirm whether the product created a duplicate control.

Fix: Add a unique accessible name or stable test id at the product boundary.

### 2. A broad CSS or text selector is being used

The selector describes appearance or copy rather than the intended control contract.

Diagnostic: Compare the current selector with getByRole, getByLabel, and getByTestId candidates.

Fix: Use the narrowest user-facing locator that remains unique.

### 3. A hidden duplicate remains in the DOM

Responsive or transitional UI may render two copies while only one is visible.

Diagnostic: Check visibility and responsive containers for every match.

Fix: Remove the duplicate from the DOM when inactive or scope the locator to the active region.

## Prevention

- Verify locator uniqueness in component tests.
- Prefer role and accessible name contracts.
- Avoid positional `.first()` as a repair.

## Primary references

- [Playwright locators](https://playwright.dev/docs/locators)

Triage this failure locally: https://automationtester.in/tools/test-failure-triage