AgentSkillsCN

playwright

通过 Playwright MCP 工具实现浏览器自动化。适用于 AI 智能体需要与网页浏览器交互的场景:(1) 网页爬取与数据提取;(2) 表单交互与提交;(3) 浏览器测试与验证;(4) 截取网页截图,或完成各类浏览器自动化任务。

SKILL.md
--- frontmatter
name: playwright
description: Browser automation via Playwright MCP tools. Use when AI Agent needs to interact with web browsers for: (1) Web scraping and data extraction, (2) Form interaction and submission, (3) Browser testing and validation, (4) Taking screenshots of web pages, or any browser automation tasks.
mcp:  
  playwright:  
    command: nix
    args: ["run", "nixpkgs#playwright-mcp"]

Playwright MCP Browser Automation

Core Workflow

code
navigate → snapshot → interact → snapshot → close

Tool Selection

ToolUse Case
playwright_browser_navigateOpen URL
playwright_browser_snapshotRead page structure (preferred over screenshot)
playwright_browser_take_screenshotCapture visual state
playwright_browser_clickClick elements
playwright_browser_typeType text into elements
playwright_browser_fill_formBatch fill multiple form fields
playwright_browser_select_optionSelect dropdown options
playwright_browser_press_keyKeyboard shortcuts
playwright_browser_dragDrag and drop
playwright_browser_hoverMouse hover
playwright_browser_evaluateExecute JavaScript
playwright_browser_file_uploadUpload files
playwright_browser_handle_dialogHandle alerts/confirms
playwright_browser_wait_forWait for text/disappear/timer
playwright_browser_tabsTab management (list/new/close/select)
playwright_browser_console_messagesCheck for errors
playwright_browser_network_requestsInspect network traffic
playwright_browser_run_codeRun Playwright code snippets

Best Practices

Order matters:

  1. navigate to URL
  2. snapshot to find elements (use ref from output)
  3. wait_for text to ensure load
  4. Interact with ref from snapshot
  5. Repeat snapshot to track changes
  6. close when done

Form filling: Use fill_form with fields array (each needs name, type, ref, value).

Element references: Always use ref from snapshot output - never guess selectors.

Dialogs: Call handle_dialog with accept: true/false before triggering action.

Errors: Check console_messages(onlyErrors=true) before completing.

Screenshots: Only use when visual inspection needed. Use snapshot for structure.

Multiple tabs: Use tabs(action="new/select/close") with index for close/select.