AgentSkillsCN

playwright

通过 Playwright MCP 实现浏览器自动化。适用于验证、浏览、网页抓取、测试、截图,以及各类浏览器交互操作。

SKILL.md
--- frontmatter
name: playwright
description: Browser automation via Playwright MCP. Use for verification, browsing, web scraping, testing, screenshots, and all browser interactions.

Playwright Skill

When to Load This Skill

  • Browser automation tasks
  • Visual verification of web applications
  • Web scraping and data extraction
  • Screenshot capture
  • End-to-end testing workflows
  • Form filling and interaction testing

Available MCP Tools

The playwright MCP provides these tools:

ToolPurpose
playwright_navigateGo to a URL
playwright_screenshotCapture the current page
playwright_clickClick an element
playwright_fillFill a form field
playwright_selectSelect dropdown option
playwright_hoverHover over element
playwright_evaluateRun JavaScript in page

Common Workflows

Navigate and Screenshot

javascript
// Navigate to URL
playwright_navigate({ url: "https://example.com" })

// Take screenshot
playwright_screenshot({ name: "homepage" })

Form Interaction

javascript
// Fill a form
playwright_fill({ selector: "#email", value: "test@example.com" })
playwright_fill({ selector: "#password", value: "secret123" })

// Click submit
playwright_click({ selector: "button[type='submit']" })

Wait for Elements

javascript
// Navigate and wait for content
playwright_navigate({ url: "https://example.com" })
playwright_evaluate({ 
  script: "document.querySelector('.loaded')?.textContent" 
})

Selector Strategies

PrioritySelector TypeExample
1data-testid[data-testid="submit-btn"]
2rolebutton[name="Submit"]
3texttext="Submit"
4CSS.btn-primary
5XPath//button[@type="submit"]

Prefer data-testid for stability.


Verification Patterns

Check Page Loaded

javascript
playwright_navigate({ url: "https://app.example.com" })
const title = playwright_evaluate({ 
  script: "document.title" 
})
// Verify title matches expected

Check Element Visible

javascript
playwright_evaluate({
  script: `
    const el = document.querySelector('.success-message');
    el && el.offsetParent !== null
  `
})

Extract Data

javascript
playwright_evaluate({
  script: `
    Array.from(document.querySelectorAll('.item'))
      .map(el => ({
        title: el.querySelector('.title')?.textContent,
        price: el.querySelector('.price')?.textContent
      }))
  `
})

Best Practices

  1. Always screenshot after important actions for evidence
  2. Use explicit waits rather than arbitrary delays
  3. Prefer stable selectors (data-testid > CSS class)
  4. Handle navigation - wait for page load before interacting
  5. Clean up - close browsers when done

Error Handling

ErrorCauseSolution
Element not foundSelector wrong or element not renderedWait for element, check selector
TimeoutPage too slowIncrease timeout, check network
Navigation failedInvalid URL or network errorVerify URL, check connectivity