AgentSkillsCN

playwright-mcp

通过 Playwright MCP 服务器实现浏览器自动化。可对网页进行导航、点击、输入、爬取、截图,以及测试。适用于自动化浏览器交互、网络爬取、E2E 测试、PDF 生成,或截图捕获时使用。可在浏览器自动化、网络爬取、playwright、无头浏览器、E2E 测试、截图、PDF 导出等场景下触发。

SKILL.md
--- frontmatter
name: playwright-mcp
description: Browser automation via Playwright MCP server. Navigate, click, type, scrape, screenshot, and test web pages. Use when automating browser interactions, web scraping, E2E testing, PDF generation, or screenshot capture. Triggers on browser automation, web scraping, playwright, headless browser, E2E testing, screenshot, PDF export.

Playwright MCP Browser Automation

Comprehensive browser automation using the Playwright MCP server integration.

Setup

Installation

bash
npx @anthropic-ai/claude-code@latest mcp add playwright -- npx @playwright/mcp@latest

Configuration Options

OptionDescriptionExample
--browserBrowser to usechrome, firefox, webkit, msedge
--headlessRun without UI--headless (default) or --no-headless
--deviceEmulate device--device="iPhone 15"
--viewportSet viewport size--viewport="1920x1080"
--capsEnable capabilities--caps=vision,pdf,testing,tracing

Core Tools

Navigation

ToolPurpose
browser_navigateNavigate to URL
browser_navigate_backGo back in history
browser_tabsManage tabs (list/new/close/select)
browser_closeClose page/browser
browser_resizeResize window

Interaction

ToolPurpose
browser_clickClick element
browser_typeType into input
browser_fill_formFill multiple form fields
browser_select_optionSelect dropdown option
browser_hoverHover over element
browser_dragDrag and drop
browser_press_keyPress keyboard key
browser_file_uploadUpload files
browser_handle_dialogHandle JS dialogs

Information Retrieval

ToolPurpose
browser_snapshotGet page accessibility tree (preferred)
browser_take_screenshotCapture visual screenshot
browser_console_messagesGet console logs
browser_network_requestsGet network requests

Utilities

ToolPurpose
browser_wait_forWait for conditions
browser_evaluateExecute JavaScript
browser_run_codeRun Playwright code
browser_installInstall browser

Common Patterns

Web Scraping

code
1. browser_navigate to target URL
2. browser_wait_for content to load
3. browser_snapshot to get page structure
4. browser_evaluate to extract data
5. Repeat for pagination if needed

Form Automation

code
1. browser_navigate to form page
2. browser_snapshot to find form fields
3. browser_fill_form with all field data
4. browser_click submit button
5. browser_wait_for success indicator

Authentication Flow

code
1. browser_navigate to login page
2. browser_snapshot to identify fields
3. browser_type username into email field
4. browser_type password into password field
5. browser_click login button
6. browser_wait_for dashboard content

Screenshot Capture

code
1. browser_navigate to target page
2. browser_wait_for content to load
3. browser_take_screenshot with options:
   - fullPage: true for entire page
   - element/ref for specific element

Element References

Elements are identified using accessibility tree references from browser_snapshot:

code
- link[Home] - Link with text "Home"
- button[Submit] - Button labeled "Submit"
- textbox[Email] - Input field labeled "Email"
- combobox[Country] - Dropdown for Country

Optional Capabilities

Vision (--caps=vision)

  • browser_mouse_click_xy - Click at coordinates
  • browser_mouse_drag_xy - Drag between coordinates
  • browser_mouse_move_xy - Move mouse

PDF (--caps=pdf)

  • browser_pdf_save - Save page as PDF

Testing (--caps=testing)

  • browser_generate_locator - Generate Playwright locator
  • browser_verify_element_visible - Verify element visibility
  • browser_verify_text_visible - Verify text on page
  • browser_verify_value - Verify element value

Tracing (--caps=tracing)

  • browser_start_tracing - Start recording trace
  • browser_stop_tracing - Stop and save trace

Best Practices

  1. Always use browser_snapshot first - Understand page structure before interacting
  2. Wait for content - Use browser_wait_for before interactions
  3. Handle dynamic content - Re-snapshot after page changes
  4. Use accessibility references - More reliable than coordinates
  5. Handle dialogs promptly - They block other actions
  6. Close browser when done - Free up resources

Troubleshooting

IssueSolution
Browser not installedUse browser_install tool
Element not foundTake fresh browser_snapshot
Timeout errorsUse browser_wait_for with longer time
Dialog blockingUse browser_handle_dialog
Stale elementRe-snapshot after page changes

When to Use This Skill

  • Automating browser interactions
  • Web scraping dynamic content
  • E2E testing workflows
  • Generating PDFs from web pages
  • Taking screenshots
  • Form filling and submission
  • Multi-page workflows