
Getting Started with Headless Chrome - uptown
https://developers.google.com/web/updates/2017/04/headless-chrome
======
llimllib
I wrote a little ipython notebook with a stupid simple example of getting
headless chrome up and talking to it via a websocket:
[https://gist.github.com/llimllib/7f6143a1a6955d243161b2fec23...](https://gist.github.com/llimllib/7f6143a1a6955d243161b2fec23b8f4d)

Then I started on a python library that would handle communication with chrome
in a real way using asyncio:
[https://twitter.com/llimllib/status/855433309375082496](https://twitter.com/llimllib/status/855433309375082496)

If that's a thing that you're interested in, let me know.
[https://github.com/llimllib/chrome-
control/tree/wstest](https://github.com/llimllib/chrome-control/tree/wstest)

~~~
fake-name
FWIW, I wrote a wrapper that exposes the remote debugger interface in python
functions here: [https://github.com/fake-
name/ChromeController](https://github.com/fake-name/ChromeController)

It works by reading the interface descripton JSON files, and dynamically
generating the API.

It doesn't handle the higher-level stuff (async, etc...), but for synchronous
things, it's quite nice.

\--------

As an aside, if you're interested in this sort of thing, the chrome devtools
protocol has a issue tracker here:
[https://github.com/ChromeDevTools/devtools-
protocol/issues](https://github.com/ChromeDevTools/devtools-protocol/issues)

Yes, it's on github, no idea why, when they already have their monorail issue
tracker.

~~~
ebidel
FWIW, we created the Github issue tracker b/c it's a bit more approachable for
everyone. Github is also a much better place for discussions.

~~~
fake-name
Ah, that makes sense.

Monowall is kind of poor. The inability to edit comments is particularly
annoying.

------
foob
I recently wrote a guide to using headless Chrome with Selenium and Chrome
WebDriver [1]. I thought that some people in this thread might find it useful
considering that the submitted article doesn't mention Selenium at all.

[1] - [https://intoli.com/blog/running-selenium-with-headless-
chrom...](https://intoli.com/blog/running-selenium-with-headless-chrome/)

~~~
xdissent
Your guide mentions setting window size and taking screenshots, which I
thought were not currently working via chromedriver. Do you know if that was
fixed or is there something else going on?

~~~
foob
Setting window size via the ChromeDriver API didn't work for me but I was able
to set it using a ChromeOptions command-line argument as I do in the guide.
Screenshots seem to work fine on Linux but I haven't tried on other platforms.

------
rsync
Off-topic, but I'm just going to start bringing this up in every thread like
this ...

We _need_ a recipe / toolchain for running a browser instance in a chroot
jail. With a head. GUI. On the desktop.

I want to fire up a "banking browser" (or "danger browser" or "social media
browser") that runs on its own IP, in its own chroot, and I can revert to
virgin snapshot state anytime I want ... and I _don 't_ want to fire up a
full-blown VM for eac of these ...

What is standing in the way of making this happen ?

Who is working on things related to this that I can donate funds to or
establish a bounty ?

~~~
noja
Sounds like Qubes OS [https://www.qubes-os.org/](https://www.qubes-os.org/)

~~~
rsync
The idea here is that I set up a chroot jail for firefox or chrome and
configure it with things like local filesystem for cookies and cache and
certs, etc.

It would also get its own unique IP, this jail.

Then I fire up firefox inside that chroot jail and use it to visit some
websites ... and then I can wipe the whole thing out and redeploy again later,
starting from scratch.

I don't need to trust incognito mode, I don't need to trust wiping cache or
tabs talking to each other (or not) and I can worry a lot less about browser
level exploits.

I can even put in firewall rules so that my "banking" instance can only talk
to boa.com and scotttrade.com (or whatever).

It's totally workable (and I have done it) with vmware. Make a "bank browsing"
VM and revert to pristine snapshot every day. The problem is that this is
terribly heavyweight and overkill when I don't need a full blown VM.

It's not even _really_ a browser issue - the real issue is, _how do you jail a
GUI application in X such that that window is in a chroot jail, distinct from
the rest of your desktop_?

~~~
developer2
>> this is terribly heavyweight and overkill when I don't need a full blown VM

The entire concept you're aiming to set up is terribly heavyweight and
overkill. If you're knowledgeable enough to be discussing VMs and chroots, you
must realize that what you are proposing is being careful to the point of
paranoia à la tinfoil hat. Those of us who know how to stay as safe as
possible via "basic" methods of security should be sleeping soundly knowing
we're already in the top 5-10% of consumers. Install OS security updates, use
a virus scanner and firewall, don't install pirated software (more likely to
contain malware), and you're better off than most people by a significant
margin.

You're talking about barely making a dent in the chances of your credentials
or sessions being compromised. Private browsing, a separate browser instance,
a VM, or chroot makes no difference if you have malware with a keylogger on
the host system. Give yourself a break, realize that there is no such thing as
"perfect security", and stop worrying so much. The amount of energy you're
pouring into "banking safely" is not a sane endeavor. It serves no useful
purpose. You could be investing this time and energy into something _far more
likely_ to improve your quality of life (eg: family, friends, health, etc.).

~~~
jjeaff
I've seen so many people stress about getting their credit card stolen or bank
accounts hacked. It's rather ridiculous considering you don't bear the
liability of a hack. If you didn't access or approve a usage of your accounts,
the banks just give it back. I have had money stolen more than once from
skimmers and I have never had any trouble getting it all back.

------
jaffoneh
Clarity just moved to headless Chrome from PhantomJS today:
[https://github.com/vmware/clarity/pull/803](https://github.com/vmware/clarity/pull/803)

~~~
orf
> Clarity Design System

> UX guidelines, HTML/CSS framework, and Angular components working together
> to craft exceptional experiences

Crashes on Firefox, causes the "Script not responding" dialogue to appear.
What a truly exceptional experience.

~~~
jaffoneh
We do have a bug on the website, it doesn't really have to do with the design
system itself but with the way the website itself is built. Should be fixed
soon. Thanks for the feedback :)

~~~
jaffoneh
In case anyway checks this out, an update to the Clarity site has already went
out that improves the performance of the website drastically. Let me know if
you're still seeing any issues and thanks again for the feedback :)

------
hackcasual
I contributed a small patch to get this working on Windows. We were a
PhantomJS shop, but it was just so unstable, thought we'd give this a shot.
Have been running on it for over 2 months now and it's dropped test failures
due to intermittent issues to near 0.

------
aantix
Capybara with headless Chrome..

[http://blog.faraday.io/headless-chromium/](http://blog.faraday.io/headless-
chromium/)

------
vanni
Related recent news: "PhantomJS: Stepping down as maintainer"

[https://news.ycombinator.com/item?id=14105489](https://news.ycombinator.com/item?id=14105489)

Also relevant: "Headless Chrome is Coming Soon" from PhantomJS dev mailing
list

[https://groups.google.com/forum/#!msg/phantomjs-
dev/S-mEBwuS...](https://groups.google.com/forum/#!msg/phantomjs-
dev/S-mEBwuSgKQ/PQdE1A5FAwAJ)

------
floatboth
No mention of WebDriver, only Chrome's devtools protocol :( There's probably a
proxy or something, but would be nice to see completely integrated native
support.

~~~
talawahdotnet
That proxy already exists. It is called ChromeDriver[1][2] and was developed
in collaboration between the Chromium and Selenium teams. It is the same way
that Selenium/Webdriver controls regular Chrome now.

It would have been nice if they at least mentioned it in the article since
Selenium is such a popular browser automation tool. They do in fact mention it
on the README page for headless chromium.

[1]
[https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver](https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver)

[2]
[https://sites.google.com/a/chromium.org/chromedriver/home](https://sites.google.com/a/chromium.org/chromedriver/home)

[3]
[https://chromium.googlesource.com/chromium/src/+/lkgr/headle...](https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md)

~~~
fake-name
The problem with ChromeDriver is that it implements Selenium's idea of what
the remote control interface should look like, and frankly speaking,
Selenium's idea has some enormous and idiotic oversights[1].

[1] [http://stackoverflow.com/questions/3492541/how-do-i-get-
the-...](http://stackoverflow.com/questions/3492541/how-do-i-get-the-response-
headers-in-selenium)

~~~
tlrobinson
ChromeDriver also has a particularly annoying race condition when triggering
clicks
[https://bugs.chromium.org/p/chromedriver/issues/detail?id=28](https://bugs.chromium.org/p/chromedriver/issues/detail?id=28)
which often only appears on slow machines such as multitenant CI services like
CircleCI ([https://circleci.com/docs/1.0/chromedriver-moving-
elements/](https://circleci.com/docs/1.0/chromedriver-moving-elements/))

------
codewithcheese
What are the pros and cons of running headless Chrome compared to running
Chrome in a virtual display driver like Xvfb?

~~~
simlevesque
Xvfb is way harder to setup. Also, I assume it uses less ressources so it
scales better.

~~~
cryptarch
I use xvfb-run on Debian and it's a breeze:

    
    
        xvfb-run -s '-screen 0 1280x1024x8' ./cucumber
    

Or are you using more advanced Xvfb features, or perhaps running inside
containers?

~~~
michaelmcmillan
In my experience it's not a breeze: [https://github.com/SeleniumHQ/docker-
selenium/issues/87#issu...](https://github.com/SeleniumHQ/docker-
selenium/issues/87#issuecomment-297944128)

------
colordrops
Will the "\--disable-gpu" flag no longer be needed in the future because
headless mode will automatically disable the GPU, or because GPU support will
be added to headless mode? I really hope it's the latter.

~~~
ebidel
It's just a bug :)
[https://news.ycombinator.com/item?id=14240460](https://news.ycombinator.com/item?id=14240460)

------
VikingCoder
Huh. So, if you can express your PNG / PDF rendering in terms of HTML, SVG,
Canvas, and WebGL... How much easier, faster, more reliable would it be to use
Headless Chrome and --screenshot, rather than other means?

~~~
joshribakoff
HTML & CSS are not really suitable for "pixel perfect" text. Even text in SVG
works just like text in an HTML document in the sense there is not a way to
control precisely where text will wrap. You'd have to disable the browser's
word wrapping & come up with your own logic for where to insert <br /> tags,
even then you have issues like browsers ignoring CSS rules because of 3rd
party browser extensions, missing fonts & such.

Look at this jQuery plugin for evidence -
[http://simplefocus.com/flowtype/](http://simplefocus.com/flowtype/)

As you resize your browser, you see the browser re-layout the page & the text
jumps around. The text wraps at different positions as you resize your browser
around. You'd have to essentially render text to an image server-side, then
scale the resulting image in the browser. And if you already have an image,
there's no need for tools like wkhtmltoX or phantom for creating an image (you
already have an image)... So headless browsers are not suitable for
rasterizing documents that contain text [in my experience].

Converting HTML to PNG/PDF is like comparing apples & oranges. The conversion
will be imperfect.

~~~
VikingCoder
> HTML & CSS are not really suitable for "pixel perfect" text.

...and a lot of people don't care.

> You'd have to disable the browser's word wrapping & come up with your own
> logic for where to insert <br /> tags

...unless you don't care.

> even then you have issues like browsers

"BrowserS"? Plural? No. This is one implementation - headless Chrome. Which
you could use to do your own rendering. There's no plural. You would use one
browser to do your headless rendering.

> You'd have to essentially render text to an image server-side

That is. What this is.

> then scale the resulting image in the browser

What? It depends entirely on what your use case is. If you want to email a
PDF, then you could use headless Chrome to turn HTML into a PDF, and then
email it.

> So headless browsers are not suitable for rasterizing documents that contain
> text

I feel like you're off in this weird space, very different from what I've
experienced. And I've experienced it again, and again, at different companies.

> Converting HTML to PNG/PDF is like comparing apples & oranges. The
> conversion will be imperfect.

...but potentially faster, lower effort, and "good enough" for many use cases.

------
RazrFalcon
Did someone try to render an SVG using it? I'm using QtWebKit for now, but
it's just a huge pain.

~~~
paulirish
Yup. Renders SVGs just as Chrome does. Really straightforward.

~~~
RazrFalcon
Can you provide a minimal example?

------
wildpeaks
If you plan to use it for automated tests, note that headless Chrome is only
available on Mac & Linux (in Chrome 59), not on Windows yet.

~~~
ebidel
Windows support is coming. You can follow along at
[https://bugs.chromium.org/p/chromium/issues/detail?id=686608](https://bugs.chromium.org/p/chromium/issues/detail?id=686608)

------
nmca
I've been using headless chrome over the last couple of months to implement a
P2P network between browsers. Worked great!

~~~
ficklepickle
That sounds interesting. Do you have any more details or a blog post you can
share?

------
callumprentice
[https://bitbucket.org/lindenlab/dullahan/src/default/README....](https://bitbucket.org/lindenlab/dullahan/src/default/README.md?at=default&fileviewer=file-
view-default)

I imagine Headless Chrome will make this obsolete shortly but just in case
anyone wants to play with it, here is Dullahan - A headless browser SDK that
uses the Chromium Embedded Framework (CEF). It is designed to make it easier
to write applications that render modern web content directly to a memory
buffer, inject synthesized mouse and keyboard events as well as interact with
features like JavaScript or cookies.

------
twothamendment
Has anyone tried to use headless chrome to detect insecure content on what you
hope is a secure page? I've only looked into it a bit, and so far failed. I'd
like to know if a visitor to
[https://example.com/foo.html](https://example.com/foo.html) in chrome will
get a warning about insecure content. I'm looking for a browser because in
some cases the insecure content is loaded by javascript from a 3rd party. I
don't know a way to do that besides fire it up in a browser and let it go.

Is there a way to get the secure/insecure status from headless chrome?

~~~
tyingq
The remote debugging feature can get to that.

See: [https://chromedevtools.github.io/debugger-protocol-
viewer/to...](https://chromedevtools.github.io/debugger-protocol-
viewer/tot/Security/)

Scroll down to InsecureContentStatus

------
danso
Was on the frontpage yesterday, with 605 points and 117 comments:
[https://news.ycombinator.com/item?id=14239194](https://news.ycombinator.com/item?id=14239194)

------
bgentry
Do any of the major CI providers make this trivial to use yet? Travis CI,
CircleCI, Buildkite, etc.?

Anybody have any positive or negative experiences to report with actually
using headless Chrome?

~~~
kinlan
I lead the DevRel team for Web and Chrome so please take anything I say with a
hint of bias....

I've found it pretty easy to use so far - [https://paul.kinlan.me/chrome-on-
home/](https://paul.kinlan.me/chrome-on-home/) and got it deployed in a number
of environments including AppEngine. We are planning on getting this deployed
in our
[WebFundamentals]([https://github.com/google/WebFundamentals/blob/master/.travi...](https://github.com/google/WebFundamentals/blob/master/.travis.yml))
Travis script to run automated testing on our dev docs site.

Some things that I found hard: no documentation on how to get it running in a
CI yet - some devs (Justin Ribeiro -
[https://hub.docker.com/r/justinribeiro/chrome-
headless/](https://hub.docker.com/r/justinribeiro/chrome-headless/)) got a
docker file all set up which helped a lot, the devtools protocol whilst it has
docs wasn't quite as simple as I had hoped and I had to guess of how to call
the node library.

------
oceanswave
If you're using Dotnetcore I've created a protocol generator here

[https://github.com/baristalabs/chrome-dev-
tools](https://github.com/baristalabs/chrome-dev-tools)

With a sample on use here

[https://github.com/baristalabs/chrome-dev-tools-
sample](https://github.com/baristalabs/chrome-dev-tools-sample)

------
rogerwang
The upcoming NW.js v0.23.0 beta version will support this usage:
[https://dl.nwjs.io/live-
build/04-29-2017/nw23-3bd4af6-4d7f95...](https://dl.nwjs.io/live-
build/04-29-2017/nw23-3bd4af6-4d7f95f-5e6428a-f2085b9/v0.23.0-beta1/)

------
davidw
So much for that:

    
    
        [0501/120318.494691:ERROR:gl_implementation.cc(245)] Failed to load /opt/google/chrome/libosmesa.so: /opt/google/chrome/libosmesa.so: cannot open shared object file: No such file or directory

~~~
ebidel
As noted, you'll need the --disable-gpu flag for now. Hopefully that will go
away around ~Chrome 60.

See

\-
[https://bugs.chromium.org/p/chromium/issues/detail?id=546953...](https://bugs.chromium.org/p/chromium/issues/detail?id=546953#c152)

\-
[https://bugs.chromium.org/p/chromium/issues/detail?id=695212](https://bugs.chromium.org/p/chromium/issues/detail?id=695212)

~~~
davidw
Be convenient if all the examples had it for those of us idly pasting stuff in
while code compiles :-)

------
elorant
How's that different from running Selenium with Chrome?

~~~
matharmin
Selenium opens a real chrome window. So it's automated, but not headless. You
will also be able to use this headless support in the new chrome to run it
without the window with selenium.

------
bostonvaulter2
Could this be used to create a screenshot service that'll automatically take
screenshots of sites? Or is that not permissible under Chrome's license?

~~~
paulirish
The Chromium license shouldn't place any restrictions on using the software
like that. Go wild.

------
shmatt
This is great progress.

I use electron in node.js (via nightmare) because it can go from headless to
visible on the fly

I wonder if the Chrome team is ever going to support something similar

------
pedalpete
Does anybody know how screenshot is triggered? Is it on DOMContentLoaded? or
some other event? Can we trigger this somehow post page load?

~~~
waingake
I needed a delay before creating a pdf maybe this is helpful ?
[https://github.com/tim-field/urlToPdf/blob/master/url-to-
pdf...](https://github.com/tim-field/urlToPdf/blob/master/url-to-pdf.js#L14) (
using via node )

------
wslh
Several years ago I tested the remote debug protocol and was insanely slow. Is
it now faster, like evaluating scripts at the console?

~~~
paulirish
I don't know we've heard any performance/latency issues before that concern
issuing script evaluation over the protocol. Were you sending hundreds of
scripts to evaluate? Or payloads far over 1MB?

Regardless, if you're having problems.. definitely file a bug and get our
attention. new.crbug.com or [https://github.com/ChromeDevTools/devtools-
protocol](https://github.com/ChromeDevTools/devtools-protocol) will do the
trick.

[I'm on the DevTools team]

------
alkoumpa
Does it work without X-server?

~~~
shif
It's headless so most likely yes.

------
sharpshadow
Who would ever think Google will release a headless browser. Stunning.

------
throwasehasdwi
why is Google doing this? They're helping everyone else build a spider that
could easily serve as the front end to a new search engine. Is this their way
of fighting back against the internet closing off? Give everyone a spider so
perfect that walling in your content becomes impractical? I like the idea of
Google lighting the match that burns down the walled gardens, but if not this
than why?

It could be anything, but I feel like there's definitely another motive to
Google unleashing something that could bite their core business.

~~~
skybrian
You're looking for strategy everywhere. People at big companies do a lot of
things and not every feature is high-level strategic.

I don't know their motivations, but the first use that comes to mind is to
make automatically testing your own web app using Chrome easier. Most teams
working on web apps could use better testing, whether they work for Google or
not.

------
mc007ibi
fantastic news, is that in any way useful for server-side rendering? i can
imagine well to pre render here or there things though

------
LoSboccacc
why does it need a whole launcher app instead of taking a script to run on the
webpage with a control api baffles me

------
tehlike
only if there was an option to render pdf and save it to disk in headless
mode.

Still great step.

~~~
spatten
scroll down a bit and look for `--print-to-pdf`.

~~~
joshribakoff
Does not work for me.

$ google-chrome --disable-gpu --headless --print-to-pdf
[https://www.chromestatus.com/](https://www.chromestatus.com/) && ls -la _.pdf
ls: cannot access '_.pdf': No such file or directory

~~~
ebidel
Make sure you're using Chrome 59. If you don't have that yet, try Chrome
Canary.

~~~
joshribakoff
The Google Chrome website installs v58 when I press download, and when I try
to install Canary:

"Chrome Canary is currently not available on the Linux platform."

~~~
TheCoelacanth
Get a build of Chromium that is more recent than this feature

~~~
source99
Any ideas on how to print landscape with no margin ?

------
jeyaganesh89
Finally. PhantomJS has a competitor now.

------
mxuribe
Now, this is handy!

