使用命令行来操作Chrome

5 min read

Headless Chrome在Chrome59中发布,用于在headless环境中运行Chrome浏览器,也就是在非Chrome环境中运行Chrome。它将Chromium和Blink渲染引擎提供的所有现代Web平台功能引入命令行。

headless如何在终端中使用:我们尝试通过终端命令打开百度 的官网

chrome  --headless --disable-gpu --remote-debugging-port=8080  https://vivo.

注意:在Mac上使用前,建议先绑定Chrome的别名

alias chrome="/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"

除此之外,还可以以命令行的形式去执行以下常见的操作:

1、打印DOM:

chrome --headless --disable-gpu --dump-dom https://baidu.com

2、创建一个PDF文件

chrome --headless --disable-gpu --print-to-pdf https://baidu.com

3、截屏

chrome --headless --disable-gpu --screenshot https://baidu.com
// 设置截屏的尺寸
chrome --headless --disable-gpu --screenshot --window-size=1280,1696 https://baidu.com