Interface ISelectors
- Namespace
- Microsoft.Playwright
- Assembly
- Microsoft.Playwright.dll
Selectors can be used to install custom selector engines. See extensibility for more information.
public interface ISelectors
Methods
RegisterAsync(string, SelectorsRegisterOptions?)
Selectors must be registered before creating the page.
**Usage**
An example of registering selector engine that queries elements based on a tag name:
using Microsoft.Playwright;
using var playwright = await Playwright.CreateAsync();
// Script that evaluates to a selector engine instance. The script is evaluated in the page context.
await playwright.Selectors.RegisterAsync("tag", new()
{
Script = @"{
// 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 = page.Locator("tag=button");
// Combine it with built-in locators.
await page.Locator("tag=div").GetByText("Click me").ClickAsync();
// Can use it in any methods supporting selectors.
int buttonCount = await page.Locator("tag=button").CountAsync();
Task RegisterAsync(string name, SelectorsRegisterOptions? options = null)
Parameters
name
stringName that is used in selectors as a prefix, e.g.
{name: 'foo'}
enablesfoo=myselectorbody
selectors. May only contain[a-zA-Z0-9_]
characters.options
SelectorsRegisterOptionsCall options
Returns
SetTestIdAttribute(string)
Defines custom attribute name to be used in GetByTestId(string). data-testid
is used by default.
void SetTestIdAttribute(string attributeName)
Parameters
attributeName
stringTest id attribute name.