Chrome CDP Controller — Chrome CDP 控制器
v1.0.0Chrome CDP 控制器工具。
详细分析 ▾
运行时依赖
版本
Initial release: control a running Chrome via CDP using Puppeteer. Includes navigate/click/fill/evaluate/screenshot/network intercept.
安装命令 点击复制
技能文档
Control and automate a Chrome browser that's already running with CDP enabled, using Puppeteer.
键 Features
- Non-intrusive: Connects 到 existing Chrome, opens 新的 标签页, operates, 然后 closes 仅 标签页
- browser stays 打开: Never closes existing tabs 或 browser
- Clean automation: 每个 task runs 在...中 own 标签页, 哪个 automatically closed 当...时 已完成
1. Chrome 带有 CDP 已启用
Chrome must be running with remote debugging enabled. If not already started:
# macOS
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222 &# Windows
"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
# Linux
google-chrome --remote-debugging-port=9222 &
2. 获取 WebSocket URL
Visit http://localhost:9222/json/version to get the webSocketDebuggerUrl, for example:
ws://127.0.0.1:9222/devtools/browser/FFA7276F8E8E51645BD2AC9BE6B79607
Or use this one-liner:
curl -s http://localhost:9222/json/version | grep -o '"webSocketDebuggerUrl":"[^"]"' | cut -d'"' -f4
3. Install Dependencies
cd chrome-cdp-controller
npm install
This installs puppeteer-core which is lightweight (doesn't download Chromium).
Usage
Command-Based Execution
Create a JSON file with commands:
commands.json:
[
{"type": "navigate", "url": "https://www.baidu.com"},
{"type": "wait", "seconds": 2},
{"type": "screenshot", "path": "/tmp/screenshot.png"},
{"type": "evaluate", "script": "document.title"}
]
Execute:
node scripts/cdp_controller.js --ws "ws://127.0.0.1:9222/devtools/browser/..." --commands commands.json
节点.js API
const { CDPController } = require('./scripts/cdp_controller.js');(async () => {
const controller = new CDPController('ws://127.0.0.1:9222/devtools/browser/...');
await controller.connect();
// Navigate
await controller.navigate('https://www.taobao.com');
// Fill form
await controller.fill('#q', 'iPhone');
await controller.press('Enter');
await controller.wait(3);
// Extract data
const result = await controller.evaluate(
Array.from(document.querySelectorAll('.item'))
.slice(0, 10)
.map(item => ({
title: item.querySelector('.title')?.textContent.trim(),
price: item.querySelector('.price')?.textContent.trim()
}))
);
console.log(result.result);
// Intercept network
await controller.startIntercept('api');
// ... perform actions ...
const responses = controller.getInterceptedResponses();
await controller.close();
})();
可用 Commands
导航
- navigate - Go 到 URL
url: Target URL
- waitUntil: "加载", "domcontentloaded", 或 "networkidle2" (默认)Interaction
- click - Click 元素
selector: CSS selector
- 超时: 超时 在...中 milliseconds (默认: 5000)- fill - Fill 表单 字段 (selects 所有, 然后 types)
selector: CSS selector
- text: Text 到 fill
- 超时: 超时 在...中 milliseconds (默认: 5000)- 类型 - 类型 text character 由 character
selector: CSS selector
- text: Text 到 类型
- 延迟: 延迟 之间 characters 在...中 ms (默认: 50)
- 超时: 超时 在...中 milliseconds (默认: 5000)- press - Press 键
键: 键 name (Enter, 标签页, 转义, etc.)Data Extraction
- get_text - 获取 text content 的 single 元素
selector: CSS selector- get_all_text - 获取 text content 的 所有 matching elements
selector: CSS selector- evaluate - Execute JavaScript 和 return 结果
script: JavaScript codeNetwork Interception
- start_intercept - 开始 intercepting network responses
url_pattern: URL pattern 到 match (substring match, e.g., "api")- get_intercepted - 获取 所有 intercepted responses
- clear_intercepted - 清除 intercepted responses 列表
Utilities
- screenshot - Take screenshot
path: 输出 file path
- fullPage: Capture 满 page (默认: 假)- wait_for_selector - Wait 对于 元素 到 appear
selector: CSS selector
- 超时: 超时 在...中 milliseconds (默认: 5000)- wait - Sleep 对于 持续时间
seconds: 数字 的 seconds 到 waitCommon Workflows
示例 1: 搜索 Taobao 对于 iPhone Prices
taobao-搜索.json:
[
{"type": "navigate", "url": "https://www.taobao.com"},
{"type": "wait", "seconds": 2},
{"type": "fill", "selector": "#q", "text": "iPhone"},
{"type": "press", "key": "Enter"},
{"type": "wait", "seconds": 5},
{"type": "evaluate", "script": "Array.from(document.querySelectorAll('.item, [class=\"Item\"]')).slice(0, 10).map(item => ({ title: item.querySelector('.title, [class=\"title\"]')?.textContent.trim().substring(0, 80), price: item.querySelector('.price, [class=\"price\"]')?.textContent.trim() }))"}
]
Run:
WS_URL=$(curl -s http://localhost:9222/json/version | grep -o '"webSocketDebuggerUrl":"[^"]*"' | cut -d'"' -f4)
node scripts/cdp_controller.js --ws "$WS_URL" --commands taobao-search.json
Note: Selectors 可能 vary. 使用 browser DevTools (F12) 到 inspect elements.
示例 2: Ask ChatGPT Question
chatgpt-ask.json:
[
{"type": "navigate", "url": "https://chat.openai.com"},
{"type": "wait_for_selector", "selector": "textarea", "timeout": 10000},
{"type": "type", "selector": "textarea", "text": "What is artificial intelligence?"},
{"type": "press", "key": "Enter"},
{"type": "wait", "seconds": 10},
{"type": "get_text", "selector": "[data-message-author-role='assistant']:last-of-type"}
]
示例 3: Intercept API Responses
intercept-api.json:
[
{"type": "start_intercept", "url_pattern": "graphql"},
{"type": "navigate", "url": "https://example.com"},
{"type": "wait", "seconds": 3},
{"type": "get_intercepted"}
]
For more examples, see references/examples.md.
Workflow
When automating browser tasks:
- 获取 WebSocket URL:
ws://127.0.0.1:9222/devtools/browser/...), 使用 directly
- 否则, 获取 从 http://localhost:9222/json/version- Determine selectors:
evaluate 带有 document.querySelector 到 test selectors
- 使用 browser DevTools (F12) 到 inspect elements- Build command sequence:
navigate
- 添加 wait 或 wait_for_selector 之间 steps
- 使用 fill 对于 表单 inputs, click 对于 buttons
- 使用 evaluate 对于 complex data extraction
- 添加 start_intercept 之前 导航 如果 monitoring network traffic- Execute:
节点 scripts/cdp_controller.js --ws "" --commands
- 解析 JSON 输出- Handle failures:
wait_for_selector
- 如果 连接 fails, 验证 Chrome running 带有 CDP 已启用Tips
- Wait 乘以: 使用
wait_for_selector代替 的 fixedwait当...时 possible - Selectors: Prefer ID (
#id) > 类 (.类) > 属性 ([attr='值']) - Network interception: 开始 intercepting 之前 navigating
- JavaScript: 使用
evaluate对于 complex data extraction - Screenshots: Useful 对于 debugging
Troubleshooting
连接 失败
- Ensure Chrome running 带有
--remote-debugging-port=9222 - 验证 WebSocket URL 正确
- Check Chrome version compatibility 带有 puppeteer-core
元素 不 Found
- 验证 selector 带有 DevTools (F12)
- 添加
wait_for_selector之前 interaction - Check 如果 元素 在...中 iframe
模块 不 Found
cd chrome-cdp-controller
npm install
免费技能或插件可能存在安全风险,如需更匹配、更安全的方案,建议联系付费定制