mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-15 22:22:53 +03:00
docs(python): add missing register selector docs (#8309)
This commit is contained in:
parent
3aae170b03
commit
cd41c34299
@ -69,11 +69,77 @@ browser.close();
|
|||||||
```
|
```
|
||||||
|
|
||||||
```python async
|
```python async
|
||||||
# FIXME: add snippet
|
import asyncio
|
||||||
|
from playwright.async_api import async_playwright
|
||||||
|
|
||||||
|
async def run(playwright):
|
||||||
|
tag_selector = """
|
||||||
|
{
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
}"""
|
||||||
|
|
||||||
|
# Register the engine. Selectors will be prefixed with "tag=".
|
||||||
|
await playwright.selectors.register("tag", tag_selector)
|
||||||
|
browser = await playwright.chromium.launch()
|
||||||
|
page = await browser.new_page()
|
||||||
|
await page.set_content('<div><button>Click me</button></div>')
|
||||||
|
|
||||||
|
# Use the selector prefixed with its name.
|
||||||
|
button = await page.query_selector('tag=button')
|
||||||
|
# Combine it with other selector engines.
|
||||||
|
await page.click('tag=div >> text="Click me"')
|
||||||
|
# Can use it in any methods supporting selectors.
|
||||||
|
button_count = await page.eval_on_selector_all('tag=button', 'buttons => buttons.length')
|
||||||
|
print(button_count)
|
||||||
|
await browser.close()
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
async with async_playwright() as playwright:
|
||||||
|
await run(playwright)
|
||||||
|
|
||||||
|
asyncio.run(main())
|
||||||
```
|
```
|
||||||
|
|
||||||
```python sync
|
```python sync
|
||||||
# FIXME: add snippet
|
from playwright.sync_api import sync_playwright
|
||||||
|
|
||||||
|
def run(playwright):
|
||||||
|
tag_selector = """
|
||||||
|
{
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
}"""
|
||||||
|
|
||||||
|
# Register the engine. Selectors will be prefixed with "tag=".
|
||||||
|
playwright.selectors.register("tag", tag_selector)
|
||||||
|
browser = playwright.chromium.launch()
|
||||||
|
page = browser.new_page()
|
||||||
|
page.set_content('<div><button>Click me</button></div>')
|
||||||
|
|
||||||
|
# Use the selector prefixed with its name.
|
||||||
|
button = page.query_selector('tag=button')
|
||||||
|
# Combine it with other selector engines.
|
||||||
|
page.click('tag=div >> text="Click me"')
|
||||||
|
# Can use it in any methods supporting selectors.
|
||||||
|
button_count = page.eval_on_selector_all('tag=button', 'buttons => buttons.length')
|
||||||
|
print(button_count)
|
||||||
|
browser.close()
|
||||||
|
|
||||||
|
with sync_playwright() as playwright:
|
||||||
|
run(playwright)
|
||||||
```
|
```
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
|
Loading…
Reference in New Issue
Block a user