diff --git a/packages/playwright-core/src/server/injected/injectedScript.ts b/packages/playwright-core/src/server/injected/injectedScript.ts index 8aedc6f0b1723..2323648bae379 100644 --- a/packages/playwright-core/src/server/injected/injectedScript.ts +++ b/packages/playwright-core/src/server/injected/injectedScript.ts @@ -478,7 +478,7 @@ export class InjectedScript { element = element.closest('button, [role=button], [role=checkbox], [role=radio]') || element; } if (behavior === 'follow-label') { - if (!element.matches('input, textarea, button, select, [role=button], [role=checkbox], [role=radio]') && + if (!element.matches('a, input, textarea, button, select, [role=link], [role=button], [role=checkbox], [role=radio]') && !(element as any).isContentEditable) { // Go up to the label that might be connected to the input/textarea. element = element.closest('label') || element; diff --git a/tests/page/retarget.spec.ts b/tests/page/retarget.spec.ts index 65daf1e205866..94e0f440e7c5e 100644 --- a/tests/page/retarget.spec.ts +++ b/tests/page/retarget.spec.ts @@ -377,3 +377,18 @@ it('check retargeting', async ({ page, asset }) => { }); } }); + +it('should not retarget anchor into parent label', async ({ page }) => { + await page.setContent(` + + `); + await page.locator('a').click(); + expect(await page.evaluate('window.__clicked')).toBe(1); + + await page.setContent(` + + + `); + await page.locator('a').click(); + expect(await page.evaluate('window.__clicked')).toBe(2); +});