Skip to main content
Version: 1.15


Selectors can be used to install custom selector engines. See Working with selectors for more information.

Selectors.RegisterAsync(name, options)#

  • name <string> Name that is used in selectors as a prefix, e.g. {name: 'foo'} enables foo=myselectorbody selectors. May only contain [a-zA-Z0-9_] characters.#
  • options <SelectorsRegisterOptions?>
    • ContentScript <bool?> Whether to run this selector engine in isolated JavaScript environment. This environment has access to the same DOM, but not any JavaScript objects from the frame's scripts. Defaults to false. Note that running as a content script is not guaranteed when this engine is used together with other registered engines.#
    • Path <string?> Script that evaluates to a selector engine instance.#
    • Script <string?> Script that evaluates to a selector engine instance.#
  • returns: <void>#

An example of registering selector engine that queries elements based on a tag name:

using var playwright = await Playwright.CreateAsync();// Script that evaluates to a selector engine instance.await playwright.Selectors.RegisterAsync("tag", @"{// 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));  }}");
await using var browser = await playwright.Chromium.LaunchAsync();var page = await browser.NewPageAsync();await page.SetContentAsync("<div><button>Click me</button></div>");// Use the selector prefixed with its name.var button = await page.QuerySelectorAsync("tag=button");// Combine it with other selector engines.await page.ClickAsync("tag=div >> text=\"Click me\"");// Can use it in any methods supporting buttonCount = await page.EvalOnSelectorAllAsync<int>("tag=button", "buttons => buttons.length");