-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_demo_async.py
More file actions
49 lines (35 loc) · 1.47 KB
/
query_demo_async.py
File metadata and controls
49 lines (35 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Example: Query engine demonstration (Async version)
"""
import asyncio
import os
from sentience.async_api import AsyncSentienceBrowser, find, query, snapshot_async
async def main():
# Get API key from environment variable (optional - uses free tier if not set)
api_key = os.environ.get("SENTIENCE_API_KEY")
async with AsyncSentienceBrowser(api_key=api_key, headless=False) as browser:
# Navigate to a page with links
await browser.goto("https://example.com", wait_until="domcontentloaded")
snap = await snapshot_async(browser)
# Query examples
print("=== Query Examples ===\n")
# Find all buttons
buttons = query(snap, "role=button")
print(f"Found {len(buttons)} buttons")
# Find all links
links = query(snap, "role=link")
print(f"Found {len(links)} links")
# Find clickable elements
clickables = query(snap, "clickable=true")
print(f"Found {len(clickables)} clickable elements")
# Find element with text containing "More"
more_link = find(snap, "text~'More'")
if more_link:
print(f"\nFound 'More' link: {more_link.text} (id: {more_link.id})")
else:
print("\nNo 'More' link found")
# Complex query: clickable links
clickable_links = query(snap, "role=link clickable=true")
print(f"\nFound {len(clickable_links)} clickable links")
if __name__ == "__main__":
asyncio.run(main())