简体   繁体   English

有无法以无头模式启动Google Chrome的方法吗?

[英]Any way to start Google Chrome in headless mode?

I carefully revised the list of switches at http://peter.sh/experiments/chromium-command-line-switches/#chrome-frame and I couldn't find anything that would launch Chrome in a hidden background process. 我在http://peter.sh/experiments/chromium-command-line-switches/#chrome-frame上仔细修改了交换机列表,我找不到任何可以在隐藏的后台进程中启动Chrome的内容。

The closest I was able to is --keep-alive-for-test + custom packaged app, but the app fails to execute any passed code because (the way it reports) "no window - ChromeHidden". 我最接近的是--keep-alive-for-test +自定义打包应用程序,但是应用程序无法执行任何传递的代码,因为(它报告的方式)“没有窗口 - ChromeHidden”。

TL;DR TL; DR

google-chrome --headless --remote-debugging-port=9222 http://example.com

You'd also need --disable-gpu temporarily. 你还需要暂时使用--disable-gpu


Tutorial : 教程

https://developers.google.com/web/updates/2017/04/headless-chrome https://developers.google.com/web/updates/2017/04/headless-chrome


There's a work in progress: https://code.google.com/p/chromium/issues/detail?id=546953 有一项工作正在进行中: https//code.google.com/p/chromium/issues/detail?id = 546953

The main deliverables are: 主要可交付成果是:

  1. A library which headless applications can link to to. 无头应用程序可以链接到的库。
  2. A sample application which demonstrates the use of headless APIs. 演示无头API使用的示例应用程序。

So it would be possible to create a simple application that runs in console without connecting to display. 因此,可以创建一个在控制台中运行而不连接到显示的简单应用程序。

Update Apr 18 '16: The work is mainly done. 更新于2016年4月18日:工作主要完成。 There's a public forum now: 现在有一个公共论坛:

https://groups.google.com/a/chromium.org/forum/#!forum/headless-dev https://groups.google.com/a/chromium.org/forum/#!forum/headless-dev

Documentation is being in progress: 文档正在进行中:

https://chromium.googlesource.com/chromium/src/+/master/headless/README.md https://chromium.googlesource.com/chromium/src/+/master/headless/README.md

Update Sep 20 '16: It looks like chrome will eventually get the "--headless" parameter: https://bugs.chromium.org/p/chromium/issues/detail?id=612904 2016年9月20日更新:看起来Chrome最终将获得“ - 无头”参数: https ://bugs.chromium.org/p/chromium/issues/detail?id = 612904

There was a presentation on BlinkOn 6 (June 16/17, 2016) 关于BlinkOn 6 的演讲 (2016年6月16日,17日)

Update Nov 29 '16: Design doc for --headless flag: https://docs.google.com/document/d/1aIJUzQr3eougZQp90bp4mqGr5gY6hdUice8UPa-Ys90/edit#heading=h.qxqfzv2lj12s 更新 --headless 11月29日:--headless flag设计doc: https--headless

Update Dec 13 '16: --headless flag is expected to be available in Canary builds soon --headless 12月13日更新: -很快将在Canary版本中提供 - --headless标志

Update Mar 12 '17: Chrome 57 has a --headless flag working. 更新--headless 3月12日: Chrome 57有一个无--headless标志工作。 Waiting for Selenium and other tools to catch up. 等待Selenium和其他工具赶上来。 User guide: https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md 用户指南: https//chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md

This guy managed to run Chrome headlessly by using Xvfb (X virtual frame buffer) to trick Chrome into thinking it was displaying a window: 这个家伙设法通过使用Xvfb(X虚拟帧缓冲区)无聊地运行Chrome,以欺骗Chrome认为它正在显示一个窗口:

http://e-method.blogspot.fr/2010/11/google-chrome-with-xvfb-headless-server.html http://e-method.blogspot.fr/2010/11/google-chrome-with-xvfb-headless-server.html

If you're on Linux you could try that. 如果你在Linux上,你可以试试。

So basically you need to install X virtual frame buffer and Google Chrome via: 所以基本上你需要通过以下方式安装X虚拟帧缓冲区和谷歌浏览器:

root@localhost: ~# apt-get install xvfb imagemagick
root@localhost: ~# apt-get install google-chrome

Then run the browser on the display: 然后在显示屏上运行浏览器:

root@localhost: ~# xvfb-run --server-args='-screen 0, 1024x768x24' \
google-chrome -start-maximized http://www.example.com \
> & /dev/null &
root@localhost: ~# DISPLAY=:99 import -window root myimage.png

或者你可以看看PhantomJS项目,这是一个无头的WebKit实现。

You could set up a linux VM and use xvfb in it. 您可以设置一个Linux VM并在其中使用xvfb。

Installation on debian / ubuntu: 在debian / ubuntu上安装:

sudo aptitude install xvfb

Start Chrome headless and visit http://example.com : 启动Chrome无头并访问http://example.com

xvfb-run --server-args='-screen 0, 1024x768x16' google-chrome 
    -start-maximized http://example.com > /dev/null &

Turns out it starts in headless mode if you start it as a child subprocess. 如果你把它作为子子进程启动,它会以无头模式启动。 Besides that: 除此之外:

  • nircmd.exe can do win hide on chrome based on its PID nircmd.exe可以根据其PID在chrome上win hide
  • Autohotkey_L can also start Chrome hidden without a taskbar button Autohotkey_L也可以在没有任务栏按钮的情况下启动Chrome隐藏

The Chromium Embedded Framework project seems like it might fit your usecase. Chromium Embedded Framework项目似乎可能适合您的用例。 I don't have personal experience with the project, but I've heard good things, and it has a solid API that you should be able to exploit for your purposes. 我没有这个项目的个人经验,但我听说过好东西,并且它有一个可靠的API,你应该能够为你的目的利用它。

I don't have enough reputation to comment yet, but want to let you guys know that the chrome headless mode which Vanuan mentions actually works with Selenium webdriver. 我还没有足够的声誉发表评论,但是想让你们知道,Vanuan提到的镀铬无头模式实际上与Selenium webdriver配合使用。

In Java you can pass the flag to chrome through chromeDriver with the following code: 在Java中,您可以使用以下代码通过chromeDriver将标志传递给chrome:

ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
ChromeDriver chromeDriver = new ChromeDriver(options);

I've recently found this article which mentions several commandline options that seem to do it. 我最近发现这篇文章提到了几个似乎可以做到的命令行选项。 Using these keywords I googled out this piece of code which seem to confirm that these options exist. 使用这些关键字,我搜索了这段代码 ,似乎证实这些选项存在。

// Does not automatically open a browser window on startup (used when
// launching Chrome for the purpose of hosting background apps).
const char kNoStartupWindow[]               = "no-startup-window";

// Causes Chrome to launch without opening any windows by default. Useful if
// one wishes to use Chrome as an ash server.
const char kSilentLaunch[]                  = "silent-launch";

I managed to successfuly run Chrome with --no-startup-window and indeed it launched without any windows. 我成功地使用--no-startup-window运行Chrome,实际上它没有任何窗口启动。 It looked like it launched properly, it spawned all typical children, but the website I tried to make it load inside didn't seem to be actually visited. 看起来它正确启动,它催生了所有典型的孩子,但我试图让它加载到里面的网站似乎并没有实际访问。 It maybe possible that this headless mode is only for running apps and not for visiting sites headless*), but it looks very promising as the normal worker tree is set up, just no windows. 这种无头模式可能只适用于运行应用程序,而不适用于无头访问网站*),但它看起来非常有前途,因为正常的工作树已经设置,只是没有窗口。

The second option --silent-launch made chrome process very silent. 第二个选项--silent-launch使chrome进程非常安静。 I didn't notice any children spawned and the process exited promptly. 我没有注意到任何生成的孩子,这个过程很快就会消失。 I doubt it'll be usable for this case. 我怀疑它是否适用于这种情况。

After I failed my attempts with these options, I focused on less sophisticated ways. 在我尝试使用这些选项失败之后,我专注于不太复杂的方法。 On the bottom of the list there are two options: 在列表的底部有两个选项:

// Specify the initial window position: --window-position=x,y
const char kWindowPosition[]                = "window-position";

// Specify the initial window size: --window-size=w,h
const char kWindowSize[]                    = "window-size";

I ran Chrome with options to move it completely out of the working area: 我运行Chrome并选择将其完全移出工作区域:

--window-size=800,600 --window-position=-800,0

and as dirty as it feels, sure it's no true headless, but still the window is out of my sight, and everything done just with chrome's startup options, without external tools sending low-level window-hide messages. 并且它感觉很脏,确定它不是真正的无头,但是窗口仍然不在我的视线范围内,而且一切都是用chrome的启动选项完成的,没有外部工具发送低级别的窗口隐藏消息。

*) yes, I know try to do weird things. *)是的,我知道尝试做奇怪的事情。 Essentially I tried to get rid of Chrome window that's kept by Karma during tests. 基本上我试图摆脱测试期间由Karma保留的Chrome窗口。 I know I can switch to PhantomJS, but I specifically needed to run them in Chrome, and the window popping out was.. eh.. disturbing in the long run. 我知道我可以切换到PhantomJS,但我特别需要在Chrome中运行它们,窗口弹出是......呃......从长远来看令人不安。

It is currently under development, you can read more information about it from here: https://chromium.googlesource.com/chromium/src/+/master/headless/README.md 它目前正在开发中,您可以从这里阅读更多相关信息: https//chromium.googlesource.com/chromium/src/+/master/headless/README.md

Headless Chromium is a library for running Chromium in a headless/server environment. Headless Chromium是一个在无头/服务器环境中运行Chromium的库。 Expected use cases include loading web pages, extracting metadata (eg, the DOM) and generating bitmaps from page contents -- using all the modern web platform features provided by Chromium and Blink. 预期的用例包括加载网页,提取元数据(例如,DOM)和从页面内容生成位图 - 使用Chromium和Blink提供的所有现代Web平台功能。

It currently works on Linux, there is a nice presentation . 它目前适用于Linux,有一个很好的演示

I was also able to make chrome headless work with NightwatchJS. 我还能用NightwatchJS制作镀铬无头工作。 Here is the config that let me use it: 这是让我使用它的配置:

    "chromeHeadless": {
  "desiredCapabilities": {
    "browserName": "chrome",
    "chromeOptions": {
      "args": ["--headless"],
      "binary": "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"
    }
  }
}

Chrome 59 has the ability to create instance as headless. Chrome 59能够将实例创建为无头。 Find the below tutorial https://www.automation99.com/2017/07/how-to-use-chrome-headless-using.html?m=1 查找以下教程https://www.automation99.com/2017/07/how-to-use-chrome-headless-using.html?m=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM