Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Chrome running in headless mode from JavaScript

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?

like image 831
Jeremy Avatar asked Jun 06 '17 18:06

Jeremy


People also ask

Can headless browser be detected?

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.

Can headless Chrome run Javascript?

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 .

How do I view headless in Chrome?

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.

How do I run Chrome in headless mode in Java?

ChromeOptions options = new ChromeOptions();options. addArguments("--headless");WebDriver driver = new ChromeDriver(options); That is all. You can run it.


2 Answers

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:

  • It looks like 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).
like image 122
Josh Lee Avatar answered Sep 28 '22 01:09

Josh Lee


You can check navigator.webdriver property which is:

The webdriver read-only property of the navigator 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 The marionette.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.

like image 38
Justinas Jakavonis Avatar answered Sep 28 '22 01:09

Justinas Jakavonis