Overview
Playwright uses selector engines to find elements in the page. The selector system is highly flexible and supports multiple strategies.Built-in Selector Engines
From server/selectors.ts:32-48:CSS Selectors
Standard CSS selectors work out of the box:CSS:Light
Pierces shadow DOM:Text Selectors
Find elements by their text content:Text selectors are normalized: trimmed and whitespace collapsed.
XPath Selectors
Role Selectors
Find elements by ARIA role (accessibility-first):button,checkbox,radio,textboxlink,heading,img,list,listitemtable,row,cell,dialog- And all ARIA roles…
Test ID Selectors
Recommended for test automation:Locator Methods
getByRole
getByText
getByLabel
getByPlaceholder
getByAltText
getByTitle
getByTestId
Combining Selectors
Chaining (>>)
Filtering with :has()
And Combinator
Or Combinator
React & Vue Selectors
Find elements by React/Vue component names:Layout Selectors
Positional
Visibility
Filtering
Strict Mode
By default, actions require exactly one matching element:Strict mode prevents accidental interactions with the wrong element.
Selector Examples
By Attribute
By Relationship
Complex Selectors
Custom Selector Engines
Register custom selector engines:Selector Best Practices
Prefer User-Facing Attributes
Prefer User-Facing Attributes
Use roles, labels, and text that users see.
Use Test IDs for Stability
Use Test IDs for Stability
Add data-testid for elements without good text or role.
Avoid XPath When Possible
Avoid XPath When Possible
CSS and text selectors are more readable and maintainable.
Be Specific But Not Brittle
Be Specific But Not Brittle
Balance specificity with maintainability.
Debugging Selectors
Selector Performance
- CSS is fastest - Browser-native
- Text selectors are slower - Require content scanning
- XPath is slowest - Complex evaluation
- Layout selectors - May require style computation
Next Steps
Auto-waiting
How Playwright waits for elements
Locators
Locator API reference
Test Isolation
Ensuring independent tests
