examples/console_logging.py
1.0 KB| 1 | from playwright.sync_api import sync_playwright |
| 2 | |
| 3 | # Example: Capturing console logs during browser automation |
| 4 | |
| 5 | url = 'http://localhost:5173' # Replace with your URL |
| 6 | |
| 7 | console_logs = [] |
| 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 | # Set up console log capture |
| 14 | def handle_console_message(msg): |
| 15 | console_logs.append(f"[{msg.type}] {msg.text}") |
| 16 | print(f"Console: [{msg.type}] {msg.text}") |
| 17 | |
| 18 | page.on("console", handle_console_message) |
| 19 | |
| 20 | # Navigate to page |
| 21 | page.goto(url) |
| 22 | page.wait_for_load_state('networkidle') |
| 23 | |
| 24 | # Interact with the page (triggers console logs) |
| 25 | page.click('text=Dashboard') |
| 26 | page.wait_for_timeout(1000) |
| 27 | |
| 28 | browser.close() |
| 29 | |
| 30 | # Save console logs to file |
| 31 | with open('/mnt/user-data/outputs/console.log', 'w') as f: |
| 32 | f.write('\n'.join(console_logs)) |
| 33 | |
| 34 | print(f"\nCaptured {len(console_logs)} console messages") |
| 35 | print(f"Logs saved to: /mnt/user-data/outputs/console.log") |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

