Chrome 无头不起作用

Chrome Headless Doesn't work

我从 developers.google 那里读到 Chrome Headless 说我们可以 运行 Google 没有 UI。引用 link :

Headless Chrome is shipping in Chrome 59. It's a way to run the Chrome browser in a headless environment. Essentially, running Chrome without chrome! It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line.

Why is that useful?

A headless browser is a great tool for automated testing and server environments where you don't need a visible UI shell. For example, you may want to run some tests against a real web page, create a PDF of it, or just inspect how the browser renders an URL.

这真是一个很棒的功能,所以我对这个很酷的功能做了一些实验。这个想法是通过从 Windows 命令提示符调用 chrome.exe 将快照作为文档站点,如下所示:

chrome --headless --disable-gpu --screenshot https://www.chromestatus.com/

按照这些站点的说明执行几次后。我什么都没有。我没有得到名称为 screenshot.png 的任何图片或屏幕截图,因为文档在 Running with --screenshot will produce a file named screenshot.png in the current working directory.

之前提到了它

从这个文件也说了版本,

Caution: Headless mode is available on Mac and Linux in Chrome 59. Windows support is coming in Chrome 60. To check what version of Chrome you have, open chrome://version.

根据之前的建议进行一些检查后,我在 Windows x64 机器上 Chrome 运行 chrome://version 得到了一些结果:

Google Chrome   62.0.3202.94 (Official Build) (64-bit) (cohort: Stable)
Revision    4fd852a98d66564c88736c017b0a0b0478e885ad-refs/branch-heads/3202@{#789} 

怎么了?我错过了什么? 谢谢

之后做一些实验。对于 --screenshot 会将图像保存在与 chrome.exe 位置相同的级别,这意味着保存在 Program Files 上。

所以我们需要将参数名称和参数与一个=

结合起来

--screenshot="D:\screen.png" 将起作用,否则 Chrome 将写入其安装文件夹。大设计缺陷,任何软件都不应该使用它的安装文件夹作为工作目录。

这是完整的论点:

chrome --headless --enable-logging --disable-gpu --screenshot="D:\screen.png" "https://www.chromestatus.com/"