docs(dotnet): selectors example (#6580)

This commit is contained in:
Anže Vodovnik 2021-05-14 16:50:15 +02:00 committed by GitHub
parent 8845484ac7
commit c9d2f6bfe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -76,6 +76,31 @@ browser.close();
# FIXME: add snippet
```
```csharp
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 selectors.
int buttonCount = await page.EvalOnSelectorAllAsync<int>("tag=button", "buttons => buttons.length");
```
### param: Selectors.register.name
- `name` <[string]>