Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Playwright refuses to click because the locator matches more than one element

Your error

Error: strict mode violation: getByRole('button') resolved to 2 elements:1 more line it can show

Fix it fast

Most common cause, 1 of 3

The role or text match is looser than you think

Give the role locator its accessible name and set exact to true, so only a case-sensitive, whole-string match counts.

Fix
await page.getByRole('button', { name: 'Save', exact: true }).click();
await page.getByText('Save', { exact: true }).click();
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Playwright locators are strict: click, fill and other single-element actions throw strict mode violation when the locator resolved to 2 or more elements. Usual reasons: a loose role or text match, one button repeated in every row, or a hidden copy. Add the accessible name with exact: true, scope by row with filter(), or add visible() (Playwright 1.63+).

  • Applies to @playwright/test 1.65, Playwright for Node.js 1.63 or later (locator.visible)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Every line under the error shows one matching element and a locator Playwright suggests for it. For a quick count, log how many elements match, how many of them are visible, and what text each one carries. Different texts point to cause 1, identical texts to cause 2, and a lower visible count to cause 3.

Run this first
const target = page.getByRole('button', { name: 'Save' });
console.log(await target.count(), await target.visible().count());
console.log(await target.allTextContents());

Then find the line you see in this list and open its fix:

  1. 1The role or text match is looser than you thinkThe matched elements have different labels, such as Save and Save draft, or a role locator has no name at all
  2. 2The same control appears once in every row or cardallTextContents returns the same label several times, for example Add to cart for each product
  3. 3A hidden copy of the element is also in the DOMcount() is higher than visible().count(), often because a mobile menu and a desktop menu are both rendered and one is hidden with CSS

What does the error look like?

  • Error: strict mode violation: getByRole('button') resolved to 2 elements:
  • strict mode violation: getByText('Save') resolved to 3 elements:

Cause 1 of 3

The role or text match is looser than you think

You seeThe matched elements have different labels, such as Save and Save draft, or a role locator has no name at all

Check

Strict mode violation is thrown here because name and text matching default to a case-insensitive substring search. A locator for Save therefore also catches Save draft and Autosave settings, and a role with no name catches every button on the page.

Check
console.log(await page.getByRole('button', { name: 'Save' }).allTextContents());

Fix

Give the role locator its accessible name and set exact to true, so only a case-sensitive, whole-string match counts. The same option exists on getByText and getByLabel.

Fix
await page.getByRole('button', { name: 'Save', exact: true }).click();
await page.getByText('Save', { exact: true }).click();

Verify

The click goes through, and the locator reports a single match.

Verify
await expect(page.getByRole('button', { name: 'Save', exact: true })).toHaveCount(1);

Confirmed by playwright.dev

Did this fix it?

Cause 2 of 3

The same control appears once in every row or card

You seeallTextContents returns the same label several times, for example Add to cart for each product

Check

Lists and tables repeat identical buttons, so a page-wide locator for Add to cart matches one per item. Playwright cannot guess which item you mean and throws instead.

Check
console.log(await page.getByRole('listitem').count());

Fix

Find the item first, narrow it with filter() by its text or by a child element, and only then look for the button inside it.

Fix
await page
  .getByRole('listitem')
  .filter({ hasText: 'Product 2' })
  .getByRole('button', { name: 'Add to cart' })
  .click();

Verify

Only the chosen item changes, and an assertion on the filtered item finds exactly one.

Verify
await expect(page.getByRole('listitem').filter({ hasText: 'Product 2' })).toHaveCount(1);

Confirmed by playwright.dev

Did this fix it?

Cause 3 of 3

A hidden copy of the element is also in the DOM

You seecount() is higher than visible().count(), often because a mobile menu and a desktop menu are both rendered and one is hidden with CSS

Check

Locators match elements whether or not they are shown. A layout that keeps a display: none copy of a link or button gives two matches even though a person sees one.

Check
console.log(await page.getByRole('link', { name: 'Pricing' }).visible().count());

Fix

Ask for visible elements only. locator.visible() exists from Playwright 1.63; on 1.51 to 1.62 use filter with the visible option.

Fix
await page.getByRole('link', { name: 'Pricing' }).visible().click();
// Playwright 1.51 to 1.62
await page.getByRole('link', { name: 'Pricing' }).filter({ visible: true }).click();

Verify

The test clicks the element on screen, and the run passes at both desktop and mobile viewport sizes.

Verify
npx playwright test

Confirmed by playwright.dev

Did this fix it?

How do I stop it coming back?

  • Write role locators with a name every time; a bare getByRole('button') is almost never unique.
  • Model repeated UI as item locator plus filter() in a page object, so tests never depend on position.
  • Leave first(), last() and nth() for lists whose order is the thing under test; Playwright's docs warn they can hit the wrong element after a page change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.