📌 背景

  • Claude Code 可以通过 MCP (Model Context Protocol) 调用 Playwright 来操作浏览器。

  • Playwright 默认会从官方 CDN 下载 Chromium,但在国内网络环境下下载常常会失败或速度极慢。

  • 本地已经安装了 Chrome / Edge,但 MCP 的 Playwright 插件默认仍然去找自带 Chromium 路径:

    C:\Users\admin\AppData\Local\ms-playwright\chromium-1179\chrome-win\chrome.exe
    

    这导致即便本地有 Edge,也无法直接使用。


⚠️ MCP + Playwright 的坑点

  1. 默认路径问题

    • MCP Playwright 插件强制使用它自己的 Chromium 路径,无法通过 .claude\config.json 覆盖。

    • 如果路径不存在或未下载,会报错:

      Failed to initialize browser: browserType.launch: Executable doesn't exist at C:\Users\admin\AppData\Local\ms-playwright\chromium-1179\chrome-win\chrome.exe
      
  2. 国内网络下载慢或失败

  3. 指定本地浏览器路径不可用

    • 虽然 .claude/config.json 可以指定 executablePath,但 MCP 插件仍然会尝试启动它管理的 Chromium。
    • 因此使用本地 Edge / Chrome 绕不开 MCP 默认路径
  4. 跳过下载

    • 可以设置 PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1 跳过下载,但这样 MCP 依然找不到默认 Chromium,会报错。

✅ 可行方案

方案 A:使用 MCP + Playwright 官方 Chromium

  1. 设置国内镜像
    Windows PowerShell:

    setx PLAYWRIGHT_DOWNLOAD_HOST https://npmmirror.com/mirrors/playwright/
    

    注意:setx 设置后需要重启 PowerShell 才生效。

    临时生效(当前窗口):

    $env:PLAYWRIGHT_DOWNLOAD_HOST="https://npmmirror.com/mirrors/playwright/"
    
  2. 下载 Chromium

    npx playwright install
    
    • 这样 MCP 插件就能找到可用的 Chromium 路径。
  3. 使用 MCP 操作浏览器

    • Claude Code 就可以直接调用:

      打开 https://www.baidu.com
      
    • Playwright MCP 会自动使用下载的 Chromium。


方案 B:使用本地 Edge / Chrome(绕开 MCP Playwright)

  • MCP 插件无法完全支持本地浏览器,需要自己写 Node.js 脚本:
const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({
    executablePath: "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe",
    headless: false
  });
  const page = await browser.newPage();
  await page.goto('https://www.baidu.com');
})();
  • Claude 可以调用这个本地脚本,而不是 MCP Playwright 插件。

⚡ Windows 一键安装脚本(MCP + 国内镜像 Chromium)

保存为 setup_claude_playwright.bat,双击运行:

@echo off
setlocal

REM 1. 设置国内镜像
setx PLAYWRIGHT_DOWNLOAD_HOST https://npmmirror.com/mirrors/playwright/
echo ✅ 国内镜像设置完成

REM 2. 安装 Playwright(MCP 需要的 Chromium)
echo 正在安装 Playwright Chromium,请耐心等待...
npx playwright install

REM 3. 创建 Claude 配置文件
set CONFIG_DIR=%USERPROFILE%\.claude
set CONFIG_FILE=%CONFIG_DIR%\config.json

if not exist "%CONFIG_DIR%" (
  mkdir "%CONFIG_DIR%"
)

(
echo {
echo   "mcpServers": {
echo     "playwright": {
echo       "command": "npx",
echo       "args": [
echo         "-y",
echo         "@modelcontextprotocol/server-playwright"
echo       ],
echo       "env": {
echo         "PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD": "1",
echo         "PLAYWRIGHT_BROWSERS_PATH": "0"
echo       }
echo     }
echo   },
echo   "playwright": {
echo     "defaultBrowser": "chromium",
echo     "headless": false
echo   }
echo }
) > "%CONFIG_FILE%"

echo ✅ Claude 配置文件已生成: %CONFIG_FILE%
echo 请重启 Claude Code 使用 Playwright MCP
pause

📌 总结

  1. MCP Playwright 插件强制依赖它管理的 Chromium,无法直接用本地 Edge / Chrome。

  2. 国内环境必须使用 镜像下载 Chromium 或绕开 MCP 自己写 Node 脚本。

  3. .claude/config.json 可用于配置 MCP 服务,但不能覆盖 Chromium 默认路径。

  4. 一键脚本可以完成:

    • 国内镜像设置
    • Chromium 下载
    • Claude MCP 配置文件生成

这样 Claude 就可以正常使用 Playwright MCP 打开浏览器了。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐