# Extensibility - [Custom selector engines](#custom-selector-engines) ## Custom selector engines Playwright supports custom selector engines, registered with [selectors.register(name, script[, options])](./api.md#selectorsregistername-script-options). Selector engine should have the following properties: - `create` function to create a relative selector from `root` (root is either a `Document`, `ShadowRoot` or `Element`) to a `target` element. - `query` function to query first element matching `selector` relative to the `root`. - `queryAll` function to query all elements matching `selector` relative to the `root`. By default the engine is run directly in the frame's JavaScript context and, for example, can call an application-defined function. To isolate the engine from any JavaScript in the frame, but leave access to the DOM, register the engine with `{contentScript: true}` option. Content script engine is safer because it is protected from any tampering with the global objects, for example altering `Node.prototype` methods. All built-in selector engines run as content scripts. Note that running as a content script is not guaranteed when the engine is used together with other custom engines. An example of registering selector engine that queries elements based on a tag name: ```js // Must be a function that evaluates to a selector engine instance. const createTagNameEngine = () => ({ // Returns the first element matching given selector in the root's subtree. query(root, selector) { return root.querySelector(selector); }, // Returns all elements matching given selector in the root's subtree. queryAll(root, selector) { return Array.from(root.querySelectorAll(selector)); } }); // Register the engine. Selectors will be prefixed with "tag=". await selectors.register('tag', createTagNameEngine); // Now we can use 'tag=' selectors. const button = await page.$('tag=button'); // We can combine it with other selector engines using `>>` combinator. await page.click('tag=div >> span >> "Click me"'); // We can use it in any methods supporting selectors. const buttonCount = await page.$$eval('tag=button', buttons => buttons.length); ``` [Playwright]: api.md#class-playwright "Playwright" [Browser]: api.md#class-browser "Browser" [BrowserContext]: api.md#class-browsercontext "BrowserContext" [Page]: api.md#class-page "Page" [Frame]: api.md#class-frame "Frame" [ElementHandle]: api.md#class-elementhandle "ElementHandle" [JSHandle]: api.md#class-jshandle "JSHandle" [ConsoleMessage]: api.md#class-consolemessage "ConsoleMessage" [Dialog]: api.md#class-dialog "Dialog" [Download]: api.md#class-download "Download" [Video]: api.md#class-video "Video" [FileChooser]: api.md#class-filechooser "FileChooser" [Keyboard]: api.md#class-keyboard "Keyboard" [Mouse]: api.md#class-mouse "Mouse" [Touchscreen]: api.md#class-touchscreen "Touchscreen" [Request]: api.md#class-request "Request" [Response]: api.md#class-response "Response" [Selectors]: api.md#class-selectors "Selectors" [Route]: api.md#class-route "Route" [WebSocket]: api.md#class-websocket "WebSocket" [TimeoutError]: api.md#class-timeouterror "TimeoutError" [Accessibility]: api.md#class-accessibility "Accessibility" [Worker]: api.md#class-worker "Worker" [BrowserServer]: api.md#class-browserserver "BrowserServer" [BrowserType]: api.md#class-browsertype "BrowserType" [Logger]: api.md#class-logger "Logger" [ChromiumBrowser]: api.md#class-chromiumbrowser "ChromiumBrowser" [ChromiumBrowserContext]: api.md#class-chromiumbrowsercontext "ChromiumBrowserContext" [ChromiumCoverage]: api.md#class-chromiumcoverage "ChromiumCoverage" [CDPSession]: api.md#class-cdpsession "CDPSession" [FirefoxBrowser]: api.md#class-firefoxbrowser "FirefoxBrowser" [WebKitBrowser]: api.md#class-webkitbrowser "WebKitBrowser" [Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array" [Buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer "Buffer" [ChildProcess]: https://nodejs.org/api/child_process.html "ChildProcess" [Element]: https://developer.mozilla.org/en-US/docs/Web/API/element "Element" [Error]: https://nodejs.org/api/errors.html#errors_class_error "Error" [EvaluationArgument]: #evaluationargument "Evaluation Argument" [Map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map "Map" [Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object" [Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise" [RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp "RegExp" [Serializable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable" [UIEvent.detail]: https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail "UIEvent.detail" [URL]: https://nodejs.org/api/url.html "URL" [USKeyboardLayout]: ../src/usKeyboardLayout.ts "USKeyboardLayout" [UnixTime]: https://en.wikipedia.org/wiki/Unix_time "Unix Time" [boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean" [function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function" [iterator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols "Iterator" [null]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null "null" [number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number" [origin]: https://developer.mozilla.org/en-US/docs/Glossary/Origin "Origin" [selector]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors "selector" [Readable]: https://nodejs.org/api/stream.html#stream_class_stream_readable "Readable" [string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "string" [xpath]: https://developer.mozilla.org/en-US/docs/Web/XPath "xpath"