Skip to main content

附上

🌐 Attach

连接到现有的浏览器,而不是启动一个新的浏览器。

🌐 Connect to an existing browser instead of launching a new one.

命令

🌐 Commands

命令描述
attach --cdp=<channel>通过通道名称连接到正在运行的浏览器
attach --cdp=<url>通过 Chrome 开发者工具协议端点连接
attach --endpoint=<url>连接到 Playwright 服务器端点
attach --extension通过 Playwright 扩展连接(默认使用 Chrome)
attach --extension=<channel>通过 Playwright 扩展连接到特定通道

按通道名称附加

🌐 Attach by channel name

通过其通道名称连接到正在运行的 Chrome 或 Edge 实例。浏览器必须启用远程调试——在目标浏览器中导航到 chrome://inspect/#remote-debugging 并勾选“允许此浏览器实例进行远程调试”。

🌐 Connect to a running Chrome or Edge instance by its channel name. The browser must have remote debugging enabled — navigate to chrome://inspect/#remote-debugging in the target browser and check "Allow remote debugging for this browser instance".

Chrome remote debugging settings at chrome://inspect/#remote-debugging
# Attach to Chrome
playwright-cli attach --cdp=chrome

# Attach to Chrome Canary
playwright-cli attach --cdp=chrome-canary

# Attach to Microsoft Edge
playwright-cli attach --cdp=msedge

# Attach to Edge Dev
playwright-cli attach --cdp=msedge-dev

支持的通道:chromechrome-betachrome-devchrome-canarymsedgemsedge-betamsedge-devmsedge-canary

🌐 Supported channels: chrome, chrome-beta, chrome-dev, chrome-canary, msedge, msedge-beta, msedge-dev, msedge-canary.

这是最简单的连接方式——无需使用特殊标志启动 Chrome,也无需知道调试端口。

🌐 This is the simplest way to connect — no need to start Chrome with special flags or know the debugging port.

CDP 端点

🌐 CDP endpoint

连接到任何具有 Chrome DevTools 协议端点的基于 Chromium 的浏览器:

🌐 Connect to any Chromium-based browser with a Chrome DevTools Protocol endpoint:

# Start Chrome with remote debugging
google-chrome --remote-debugging-port=9222

# Connect from the CLI
playwright-cli attach --cdp=http://localhost:9222
playwright-cli snapshot
playwright-cli click e5

适用于:

🌐 Works with:

  • 带有 --remote-debugging-port 的 Chrome / Chromium
  • 启用远程调试的 Edge
  • 暴露 CDP 的 Electron 应用
  • 云浏览器服务(Browserbase 等)

Playwright 服务器端点

🌐 Playwright server endpoint

连接到远程 Playwright 服务器:

🌐 Connect to a remote Playwright server:

playwright-cli attach --endpoint=ws://localhost:3000
playwright-cli snapshot

浏览器扩展

🌐 Browser extension

使用 Playwright 扩展 连接到你现有的浏览器标签页。这可以让你重用已登录的会话、Cookies 和已安装的扩展程序。

🌐 Connect to your existing browser tabs using the Playwright Extension. This lets you reuse your logged-in sessions, cookies, and installed extensions.

Playwright Extension in Chrome Web Store
# Attach to Chrome (default)
playwright-cli attach --extension

# Attach to a specific channel
playwright-cli attach --extension=chrome-canary
playwright-cli attach --extension=msedge
playwright-cli attach --extension=msedge-dev

如果未指定通道,该扩展默认附加到 Chrome。

🌐 If no channel is specified, the extension attaches to Chrome by default.

何时使用扩展模式

🌐 When to use extension mode

  • SSO / 2FA — 通过重复使用已认证的会话来跳过复杂的登录流程
  • 浏览器扩展 — 与依赖已安装扩展的页面进行交互
  • 已有标签页 — 自动化你已经打开的页面

命名会话

🌐 Named sessions

Attach 创建一个会话。使用 -s 来命名它:

🌐 Attach creates a session. Use -s to name it:

playwright-cli attach --cdp=chrome -s=debug-session
playwright-cli -s=debug-session snapshot
playwright-cli -s=debug-session click e5

工作流:连接到正在运行的 Chrome

🌐 Workflow: connect to your running Chrome

# 1. In Chrome, go to chrome://inspect/#remote-debugging
# and enable "Allow remote debugging for this browser instance"

# 2. Attach by channel name
playwright-cli attach --cdp=chrome

# 3. Interact with your existing tabs
playwright-cli snapshot
playwright-cli screenshot --filename=current-state.png

# 4. Save state for future headless sessions
playwright-cli state-save auth.json

工作流程:调试远程浏览器

🌐 Workflow: debugging a remote browser

# On the remote machine: start Chrome with debugging
google-chrome --remote-debugging-port=9222

# On your machine: connect via SSH tunnel
ssh -L 9222:localhost:9222 user@remote-host

# Attach and inspect
playwright-cli attach --cdp=http://localhost:9222
playwright-cli snapshot
playwright-cli screenshot --filename=remote-state.png
playwright-cli console error