1

我正在尝试使用 cUrl 连接到 Chrome 远程调试,但是,来自 Chrome 的响应返回“200 OK”但没有数据。

我使用以下方法设置 chrome:

chrome.exe --headless --remote-debugging-port=12345

我可以成功地获得一个页面列表:

>curl -i "http://localhost:12345/json"

HTTP/1.1 200 OK
Content-Length:612
Content-Type:application/json; charset=UTF-8
[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:12345/devtools/page/19d24d3a-25b7-4ee8-a5cf-4f3d17778575",
  ...
   "webSocketDebuggerUrl": "ws://localhost:12345/devtools/page/19d24d3a-25b7-4ee8-a5cf-4f3d17778575"
} ]

但是,我只能从调试器 URL 获得空的成功响应:

>echo {"id":0,"method":"Page.navigate","params":{"url":"https://stackoverflow.com/"}}|curl -i "http://localhost:12345/devtools/page/19d24d3a-25b7-4ee8-a5cf-4f3d17778575" -H "Content-Type: application/json" -d -

HTTP/1.1 200 OK
Content-Length:0
Content-Type:text/plain

类似Page.navigate和错误请求的命令返回200 OK,但没有执行任何操作。

我错过了什么???

4

1 回答 1

3

您需要使用 websockets 来管理这样的 chrome。引用chromedevtools.github.io

您的应用程序可以通过以下请求来发现可用页面: http://localhost:9222/json并获取一个 JSON 对象,其中包含有关可检查页面的信息以及您可以用来开始检测它们的 WebSocket 地址

这就是它的工作方式(至少在我的 Mac 上):

运行 Chrome

docker pull deepsweet/chromium-headless-remote:69
docker run -it --rm -p 9222:9222 deepsweet/chromium-headless-remote:69

获取 WebSocket 地址

curl -i "http://localhost:9222/json"
HTTP/1.1 200 OK
Content-Length:361
Content-Type:application/json; charset=UTF-8

[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF",
   "id": "DC33B65CA373BE2770F2A1031C3B4CBF",
   "title": "about:blank",
   "type": "page",
   "url": "about:blank",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF"
} ]

发送你的命令

echo '{ "id":2, "method":"Page.navigate", "params":{"url": "http://www.stackoverflow.com"} }' | websocat -t - ws://localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF

示例中使用了websocat。您也可以编写一个简单的脚本来执行此操作

确保页面已打开

curl -i "http://localhost:9222/json"
HTTP/1.1 200 OK
Content-Length:432
Content-Type:application/json; charset=UTF-8

[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF",
   "id": "DC33B65CA373BE2770F2A1031C3B4CBF",
   "title": "Stack Overflow - Where Developers Learn, Share, & Build Careers",
   "type": "page",
   "url": "https://stackoverflow.com/",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF"
} ]
于 2018-10-18T19:15:46.530 回答