背景

microsoft/playwright-cli: CLI for common Playwright actions. Record and generate Playwright code, inspect selectors and take screenshots.

如果是root用户安装后默认使用会提示

Chromium sandboxing failed!
================================
To avoid the sandboxing issue, do either of the following:
  - (preferred): Configure your environment to support sandboxing
  - (alternative): Launch Chromium without sandbox using 'chromiumSandbox: false' option

解决方法:

来自grok heavy搜索得到()

在 root 下运行 playwright-cli 的正确方法(解决 Chromium sandbox 问题)

你遇到的核心问题是:Chromium 不允许以 root 用户身份运行,除非显式禁用 sandbox(错误来源:Running as root without --no-sandbox is not supported)。
你之前用的 PLAYWRIGHT_CHROMIUM_SANDBOX=false 是错误的变量名,这个 CLI 不认它。

@playwright/cli(微软官方为 AI Agent 设计的 CLI)提供了专用的环境变量来控制 sandbox。

方法1(全局)

把下面这行加到 ~/.bashrc~/.zshrc 里,以后就不用每次都敲了:

export PLAYWRIGHT_MCP_NO_SANDBOX=true

然后 source ~/.bashrc 生效。

方法2(项目级别)

playwright-cli 支持配置文件,可以直接设置 chromiumSandbox: false(官方推荐写法):

  1. 在当前目录(或项目根目录)创建配置文件:
mkdir -p .playwright
cat > .playwright/cli.config.json << EOF
{
  "browser": {
    "launchOptions": {
      "chromiumSandbox": false
    }
  }
}
EOF
  1. 之后直接运行(不需要环境变量):
playwright-cli open https://demo.playwright.dev/todomvc/ --headed

配置文件路径也可以自定义:PLAYWRIGHT_MCP_CONFIG=/path/to/config.json

Last modification:March 15, 2026
如果觉得我的文章对你有用,请随意赞赏