examples/element_discovery.py
1.4 KB| 1 | from playwright.sync_api import sync_playwright |
| 2 | |
| 3 | # Example: Discovering buttons and other elements on a page |
| 4 | |
| 5 | with sync_playwright() as p: |
| 6 | browser = p.chromium.launch(headless=True) |
| 7 | page = browser.new_page() |
| 8 | |
| 9 | # Navigate to page and wait for it to fully load |
| 10 | page.goto('http://localhost:5173') |
| 11 | page.wait_for_load_state('networkidle') |
| 12 | |
| 13 | # Discover all buttons on the page |
| 14 | buttons = page.locator('button').all() |
| 15 | print(f"Found {len(buttons)} buttons:") |
| 16 | for i, button in enumerate(buttons): |
| 17 | text = button.inner_text() if button.is_visible() else "[hidden]" |
| 18 | print(f" [{i}] {text}") |
| 19 | |
| 20 | # Discover links |
| 21 | links = page.locator('a[href]').all() |
| 22 | print(f"\nFound {len(links)} links:") |
| 23 | for link in links[:5]: # Show first 5 |
| 24 | text = link.inner_text().strip() |
| 25 | href = link.get_attribute('href') |
| 26 | print(f" - {text} -> {href}") |
| 27 | |
| 28 | # Discover input fields |
| 29 | inputs = page.locator('input, textarea, select').all() |
| 30 | print(f"\nFound {len(inputs)} input fields:") |
| 31 | for input_elem in inputs: |
| 32 | name = input_elem.get_attribute('name') or input_elem.get_attribute('id') or "[unnamed]" |
| 33 | input_type = input_elem.get_attribute('type') or 'text' |
| 34 | print(f" - {name} ({input_type})") |
| 35 | |
| 36 | # Take screenshot for visual reference |
| 37 | page.screenshot(path='/tmp/page_discovery.png', full_page=True) |
| 38 | print("\nScreenshot saved to /tmp/page_discovery.png") |
| 39 | |
| 40 | browser.close() |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

