Version
1.56.1
Steps to reproduce
- Run Codegen: npx playwright codegen https://www.flaticon.com/authors/smashicons
- Click on the magnify icon button
- Observe the generated locator: await page.getByRole('button', { name: '' }).nth(1).click();
Expected behavior
CodeGen should fall back to more reliable selectors when buttons lack meaningful text content:
Option 1: CSS selector with classes
await page.locator('button.search__button').click();
Actual behavior
When using CodeGen on buttons with only icon content and no visible text:
Invalid locator with empty name attribute
await page.getByRole('button', { name: '' }).nth(1).click();
This locator is problematic because:
- An empty name selector is not semantically meaningful
- It will match any button without an accessible name, causing ambiguity
- It doesn't uniquely identify the target element
- The locator fails to leverage accessible attributes like 'title' or 'aria-label' when available
Additional context
The issue occurs because:
- Icon fonts render glyphs via CSS pseudo-elements that contribute to accessible name calculation
- These glyphs are often Unicode private use area characters or special symbols
- After text normalization/sanitization, they become empty strings
- CodeGen doesn't detect this edge case and generates invalid selectors
A proper fix would:
- Detect when icon elements contribute only non-meaningful content to accessible names
- Filter out icon-generated text from element text extraction
- Skip role-based selectors when no meaningful text remains
- Fall back to CSS selectors or parent-scoped locators for better uniqueness
Environment
Playwright Version: v1.56.0-next
Operating System: Windows 10
Browser: All (Chromium, Firefox, WebKit)
Node.js version: 22.16.0
Version
1.56.1
Steps to reproduce
Expected behavior
CodeGen should fall back to more reliable selectors when buttons lack meaningful text content:
Option 1: CSS selector with classes
await page.locator('button.search__button').click();
Actual behavior
When using CodeGen on buttons with only icon content and no visible text:
Invalid locator with empty name attribute
await page.getByRole('button', { name: '' }).nth(1).click();
This locator is problematic because:
Additional context
The issue occurs because:
A proper fix would:
Environment