Implementation:Microsoft Playwright Server Selectors
Overview
Selectors is the server-side selector engine registry that manages built-in and custom selector engines, validates selector names, and parses selector strings into structured representations.
Description
The Selectors class maintains the registry of all available selector engines including built-in engines (css, xpath, text, role, etc.) and user-registered custom engines. It validates engine names, manages the test ID attribute name configuration, and provides the parseSelector method that converts selector strings into ParsedSelector objects.
Built-in engines include: css, xpath, text, id, role, data-testid, internal engines (has, has-text, chain, etc.), and framework-specific engines (_react, _vue).
Usage
One Selectors instance per browser context. Custom engines are registered via the public API.
Code Reference
Source Location
packages/playwright-core/src/server/selectors.ts (96 lines)
Class Signature
export class Selectors {
readonly guid: string;
constructor(engines: channels.SelectorEngine[], testIdAttributeName: string | undefined)
register(engine: channels.SelectorEngine): void
unregisterAll(): void
parseSelector(selector: string | ParsedSelector, strict: boolean): SelectorInfo
}
Import
import { Selectors } from './server/selectors';
I/O Contract
Inputs
engines: channels.SelectorEngine[]-- custom engines to register at creationtestIdAttributeName: string-- the attribute name for test IDs (default:data-testid)
Outputs
- Parsed selector structures with engine name, body, and world information
- Validation errors for invalid selector names or unregistered engines
Related Pages
- Microsoft_Playwright_FrameSelectors -- Frame-level selector resolution
- Microsoft_Playwright_CssParser -- CSS selector parsing
- Microsoft_Playwright_LocatorParser -- Locator string parsing