Extensibility
Custom selector engines#
Playwright supports custom selector engines, registered with selectors.register(name, script[, options]).
Selector engine should have the following properties:
createfunction to create a relative selector fromroot(root is either aDocument,ShadowRootorElement) to atargetelement.queryfunction to query first element matchingselectorrelative to theroot.queryAllfunction to query all elements matchingselectorrelative to theroot.
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: