mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-15 06:02:57 +03:00
docs(dotnet): selectors example (#6580)
This commit is contained in:
parent
8845484ac7
commit
c9d2f6bfe0
@ -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]>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user