跳到主要内容

网页应用测试

收藏0

用 Python Playwright 脚本测试本地网页应用:起服务、等 networkidle、先侦察 DOM 再操作,附元素发现、静态页面与控制台日志三个示例。

让 AI 安装

星光尘缘LV2最新版本 0.1.0更新于 2026-11-11下载 0 次

examples/static_html_automation.py

953 B
1from playwright.sync_api import sync_playwright
2import os
3
4# Example: Automating interaction with static HTML files using file:// URLs
5
6html_file_path = os.path.abspath('path/to/your/file.html')
7file_url = f'file://{html_file_path}'
8
9with 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
33print("Static HTML automation completed!")

文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。