With the release of Chrome 59, "headless" mode is now available in stable builds for Linux and macOS (and soon also Windows with Chrome 60). This allows us to run a full-featured version of Chrome without any visible UI, a great capability to have for automated testing. Here are examples.
chrome --headless --disable-gpu --dump-dom https://stackoverflow.com/
In my JavaScript test runner, I like to record as much information as possible about the browser being used, to help isolate issues. For example, I record many of the properties of navigator
, including the current browser plugins:
JSON.stringify(Array.from(navigator.plugins).map(p => p.name))
["Chrome PDF Viewer","Widevine Content Decryption Module","Shockwave Flash","Native Client","Chrome PDF Viewer"]
My understanding is that Chrome should behave identically in headless mode, but I have enough experience to be skeptical of a new feature that may significantly change the rendering pipeline.
For now, I am going to run tests in both modes. I would like to the test runner to record whether headless mode is being used. I could pass this information in the test configurations, but I'd rather have a pure JavaScript solution that I can build into the test runner itself. However, I haven't been able to find any browser interface that reveals whether headless mode is active.
Is there any way to detect if Chrome is running in headless mode from JavaScript?
There are many ways to detect whether a request is coming from a headless browser, but whether it will be easy to do so depends greatly on how the headless browser is configured. If used for web scraping, hackers do their absolute best to obscure detection.
You're looking for Puppeteer, an API for headless Chrome/Chromium. Once you have your script (the docs are good), you can run it with node script. js .
As we have already seen, you just have to add the flag –headless when you launch the browser to be in headless mode. With CLI (Command Line Interface), just write: chrome \<br> – headless \ # Runs Chrome in headless mode. <br> – disable-gpu \ # Temporarily needed if running on Windows.
ChromeOptions options = new ChromeOptions();options. addArguments("--headless");WebDriver driver = new ChromeDriver(options); That is all. You can run it.
The user agent string includes HeadlessChrome
instead of Chrome
. This is probably the signal that you're intended to look for, so you could use:
/\bHeadlessChrome\//.test(navigator.userAgent)
Other interesting signals include:
window.chrome
is undefined when headless.[innerWidth, innerHeight]
is [800, 600]
(hardcoded in headless_browser.cc
), while [outerWidth, outerHeight]
is [0, 0]
(which shouldn't usually happen).You can check navigator.webdriver
property which is:
The
webdriver
read-only property of thenavigator
interface indicates whether the user agent is controlled by automation....
The
navigator.webdriver
property is true when in:Chrome The
--enable-automation
or the--headless
flag is used.
Firefox Themarionette.enabled
preference or--marionette
flag is passed.
The W3C WebDriver Recommendation describes it as follows:
navigator.webdriver
Defines a standard way for co-operating user agents to inform the document that it is controlled by WebDriver, for example so that alternate code paths can be triggered during automation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With