通过 CDP 连接本机 Edge/Chrome,实现网页截图、内容提取和自动化操作。适用于打开网页并截图、提取动态内容、查看JS渲染页面及自动化浏览器操作。
通过 browser-harness CLI 控制用户本机浏览器(Edge 或 Chrome)。
本机已安装 browser-harness: bash
git clone https://github.com/browser-use/browser-harness
cd browser-harness
uv tool install -e .
浏览器调试模式启动: bash
# Edge(推荐):
"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222
# 或 Chrome:
"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
设置环境变量(每次使用前): bash
$env:BU_CDP_URL="http://127.0.0.1:9222"
bash
browser-harness -c "new_tab('https://example.com'); wait_for_load(); capture_screenshot()"
截图保存到 C:\Users\he\AppData\Local\Temp\shot.png,用完后复制到工作目录分析。
bash
browser-harness -c "new_tab('https://example.com')" # 新标签页打开
browser-harness -c "goto_url('https://example.com')" # 在当前标签页打开
bash
browser-harness -c "new_tab('https://example.com'); wait_for_load()"
bash
browser-harness -c "print(page_info())" # URL、标题、尺寸
browser-harness -c "js('document.body.innerText')" # 页面文字内容
bash
browser-harness -c "js('document.title')" # 标题
browser-harness -c "js('document.querySelectorAll(\"a\").length')" # 链接数量
bash
browser-harness -c "close_tab()"
new_tab(),不要用 goto_url()(会覆盖用户当前页面)C:\Users\he\AppData\Local\Temp\shot.png 复制出来分析wait_for_load() 在导航后必须调用web_fetch 抓不到的页面可以用这个BROWSER_USE_API_KEY 和 start_remote_daemon() 可用远程浏览器bash
# 检查调试端口是否开启
Invoke-RestMethod "http://localhost:9222/json/version"
# 有返回说明端口正常
bash
Stop-Process -Name msedge -Force
Start-Process "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222