chrome-cdp
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseChrome CDP
Chrome CDP
Lightweight Chrome DevTools Protocol CLI. Connects directly via WebSocket — no Puppeteer, works with 100+ tabs, instant connection.
轻量级Chrome DevTools Protocol命令行工具(CLI)。通过WebSocket直接连接——无需Puppeteer,支持100+标签页,连接即时。
Prerequisites
前置条件
- Chrome with remote debugging enabled: open and toggle the switch
chrome://inspect/#remote-debugging - Node.js 22+ (uses built-in WebSocket)
- 启用远程调试的Chrome浏览器:打开并开启开关
chrome://inspect/#remote-debugging - Node.js 22+(使用内置WebSocket)
Commands
命令
All commands use . The is a unique targetId prefix from ; copy the full prefix shown in the output (for example ). The CLI rejects ambiguous prefixes.
scripts/cdp.mjs<target>listlist6BE827FA所有命令均使用。是命令输出中唯一的targetId前缀;复制输出中显示的完整前缀(例如)。CLI会拒绝模糊的前缀。
scripts/cdp.mjs<target>listlist6BE827FAList open pages
列出已打开的页面
bash
scripts/cdp.mjs listbash
scripts/cdp.mjs listTake a screenshot
截取屏幕截图
bash
scripts/cdp.mjs shot <target> [file] # default: /tmp/screenshot.pngCaptures the viewport only. Scroll first with if you need content below the fold. Output includes the page's DPR and coordinate conversion hint (see Coordinates below).
evalbash
scripts/cdp.mjs shot <target> [file] # 默认值:/tmp/screenshot.png仅捕获视口区域。如果需要捕获折叠内容,请先使用命令滚动页面。输出内容包含页面的DPR(设备像素比)和坐标转换提示(见下方坐标部分)。
evalAccessibility tree snapshot
可访问性树快照
bash
scripts/cdp.mjs snap <target>bash
scripts/cdp.mjs snap <target>Evaluate JavaScript
执行JavaScript代码
bash
scripts/cdp.mjs eval <target> <expr>Watch out: avoid index-based selection () across multiplequerySelectorAll(...)[i]calls when the DOM can change between them (e.g. after clicking Ignore, card indices shift). Collect all data in oneevalor use stable selectors.eval
bash
scripts/cdp.mjs eval <target> <expr>注意: 当DOM在多次调用之间可能发生变化时(例如点击“忽略”后,卡片索引会偏移),避免使用基于索引的选择器(eval)。建议在单次querySelectorAll(...)[i]调用中收集所有数据,或使用稳定的选择器。eval
Other commands
其他命令
bash
scripts/cdp.mjs html <target> [selector] # full page or element HTML
scripts/cdp.mjs nav <target> <url> # navigate and wait for load
scripts/cdp.mjs net <target> # resource timing entries
scripts/cdp.mjs click <target> <selector> # click element by CSS selector
scripts/cdp.mjs clickxy <target> <x> <y> # click at CSS pixel coords
scripts/cdp.mjs type <target> <text> # Input.insertText at current focus; works in cross-origin iframes unlike eval
scripts/cdp.mjs loadall <target> <selector> [ms] # click "load more" until gone (default 1500ms between clicks)
scripts/cdp.mjs evalraw <target> <method> [json] # raw CDP command passthrough
scripts/cdp.mjs stop [target] # stop daemon(s)bash
scripts/cdp.mjs html <target> [selector] # 完整页面或元素的HTML代码
scripts/cdp.mjs nav <target> <url> # 导航至指定URL并等待加载完成
scripts/cdp.mjs net <target> # 资源计时条目
scripts/cdp.mjs click <target> <selector> # 通过CSS选择器点击元素
scripts/cdp.mjs clickxy <target> <x> <y> # 点击指定CSS像素坐标位置
scripts/cdp.mjs type <target> <text> # 在当前焦点位置执行Input.insertText;与eval不同,此命令可在跨源iframe中生效
scripts/cdp.mjs loadall <target> <selector> [ms] # 点击“加载更多”按钮直到其消失(默认两次点击间隔1500ms)
scripts/cdp.mjs evalraw <target> <method> [json] # 直接传递原始CDP命令
scripts/cdp.mjs stop [target] # 停止守护进程(可指定目标)Coordinates
坐标
shotclickxyCSS px = screenshot image px / DPRshotshotclickxyCSS像素 = 截图图片像素 / DPRshotTips
提示
- Prefer over
snap --compactfor page structure.html - Use (not eval) to enter text in cross-origin iframes —
type/clickto focus first, thenclickxy.type - Chrome shows an "Allow debugging" modal once per tab on first access. A background daemon keeps the session alive so subsequent commands need no further approval. Daemons auto-exit after 20 minutes of inactivity.
- 如需获取页面结构,优先使用而非
snap --compact命令。html - 在跨源iframe中输入文本时,使用命令(而非eval)——先使用
type/click命令聚焦输入框,再执行clickxy命令。type - Chrome会在首次访问每个标签页时显示“允许调试”弹窗。后台守护进程会保持会话活跃,因此后续命令无需再次获得批准。守护进程在闲置20分钟后会自动退出。