examples/static_html_automation.py
953 B| 1 | from playwright.sync_api import sync_playwright |
| 2 | import os |
| 3 | |
| 4 | # Example: Automating interaction with static HTML files using file:// URLs |
| 5 | |
| 6 | html_file_path = os.path.abspath('path/to/your/file.html') |
| 7 | file_url = f'file://{html_file_path}' |
| 8 | |
| 9 | with sync_playwright() as p: |
| 10 | browser = p.chromium.launch(headless=True) |
| 11 | page = browser.new_page(viewport={'width': 1920, 'height': 1080}) |
| 12 | |
| 13 | # Navigate to local HTML file |
| 14 | page.goto(file_url) |
| 15 | |
| 16 | # Take screenshot |
| 17 | page.screenshot(path='/mnt/user-data/outputs/static_page.png', full_page=True) |
| 18 | |
| 19 | # Interact with elements |
| 20 | page.click('text=Click Me') |
| 21 | page.fill('#name', 'John Doe') |
| 22 | page.fill('#email', '[email protected]') |
| 23 | |
| 24 | # Submit form |
| 25 | page.click('button[type="submit"]') |
| 26 | page.wait_for_timeout(500) |
| 27 | |
| 28 | # Take final screenshot |
| 29 | page.screenshot(path='/mnt/user-data/outputs/after_submit.png', full_page=True) |
| 30 | |
| 31 | browser.close() |
| 32 | |
| 33 | print("Static HTML automation completed!") |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

