element_discovery.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from playwright.sync_api import sync_playwright
  2. # Example: Discovering buttons and other elements on a page
  3. with sync_playwright() as p:
  4. browser = p.chromium.launch(headless=True)
  5. page = browser.new_page()
  6. # Navigate to page and wait for it to fully load
  7. page.goto('http://localhost:5173')
  8. page.wait_for_load_state('networkidle')
  9. # Discover all buttons on the page
  10. buttons = page.locator('button').all()
  11. print(f"Found {len(buttons)} buttons:")
  12. for i, button in enumerate(buttons):
  13. text = button.inner_text() if button.is_visible() else "[hidden]"
  14. print(f" [{i}] {text}")
  15. # Discover links
  16. links = page.locator('a[href]').all()
  17. print(f"\nFound {len(links)} links:")
  18. for link in links[:5]: # Show first 5
  19. text = link.inner_text().strip()
  20. href = link.get_attribute('href')
  21. print(f" - {text} -> {href}")
  22. # Discover input fields
  23. inputs = page.locator('input, textarea, select').all()
  24. print(f"\nFound {len(inputs)} input fields:")
  25. for input_elem in inputs:
  26. name = input_elem.get_attribute('name') or input_elem.get_attribute('id') or "[unnamed]"
  27. input_type = input_elem.get_attribute('type') or 'text'
  28. print(f" - {name} ({input_type})")
  29. # Take screenshot for visual reference
  30. page.screenshot(path='/tmp/page_discovery.png', full_page=True)
  31. print("\nScreenshot saved to /tmp/page_discovery.png")
  32. browser.close()