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 showFix it fast
Most common cause, 1 of 3The 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.
await page.getByRole('button', { name: 'Save', exact: true }).click();
await page.getByText('Save', { exact: true }).click();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+).
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.
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:
- 1The role or text match is looser than you think
The matched elements have different labels, such as Save and Save draft, or a role locator has no name at all - 2The same control appears once in every row or card
allTextContents returns the same label several times, for example Add to cart for each product - 3A hidden copy of the element is also in the DOM
count() 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.
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.
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.
await expect(page.getByRole('button', { name: 'Save', exact: true })).toHaveCount(1);Confirmed by playwright.dev
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.
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.
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.
await expect(page.getByRole('listitem').filter({ hasText: 'Product 2' })).toHaveCount(1);Confirmed by playwright.dev
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.
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.
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.
npx playwright testConfirmed by playwright.dev
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.