Is it possible to run Google Chrome in headless mode with extensions?
Asked Answered
D

10

72

I cannot use my currently installed extensions in Google Chrome using headless mode. Is there any way to enable them?

An easy way to check if the extensions work is by adding, for example, the "Comic Sans Everything" extension.

So Google looks like that:

Google looks beautiful

However, if I take a screenshot of the page using the headless mode (google-chrome --headless --disable-gpu --screenshot https://www.google.com), the result is:

Google in headless mode

Deniable answered 28/7, 2017 at 11:14 Comment(2)
Not possible currently, see crbug.com/706008Extravagate
Related: Using Google Chrome's Puppeteer to load an extension - though it won't work headless.Bypass
D
46

EDIT: This answer is no longer correct, see https://mcmap.net/q/273531/-is-it-possible-to-run-google-chrome-in-headless-mode-with-extensions

No, it's not possible, and Chrome developers decided against implementing it in any near future due to complexity of the task.

If you look at that issue you may get the idea that they are still considering it due to ChromeDriver requirements - but instead they decided to make ChromeDriver work without extensions (through DevTools).

Dactylic answered 28/7, 2017 at 11:42 Comment(6)
will headless mode support extension in the future? are there any other web browser that support headless mode with extensions?Odel
Yes firefox supports them. This is a crappy missing feature.Dinosaurian
This answer is not valid anymore: --headless=chrome was added in 2021 as a way of running Chrome in headless mode without loss of features. See the comment here: bugs.chromium.org/p/chromium/issues/detail?id=706008#c36Hex
@MichaelMintz I invite you to make a new answer, would gladly upvote it.Dactylic
That newer answer has already been added by someone else.Hex
have a try "--haedless=new" for chrome version > 109 :)Corollaceous
D
48

You can run Chrome with extensions headless using Xvfb.

  1. Install Xvfb. On Fedora sudo dnf install xorg-x11-server-Xvfb
  2. xvfb-run google-chrome --remote-debugging-port=9222 --disable-gpu https://www.google.com
  3. Use chrome-remote-interface (or another Chrome Debug Protocol client) to trigger the screenshot.

More complicated, but it does work. It's what we use for headless chrome extension testing.

Drumfire answered 28/9, 2017 at 18:26 Comment(2)
Just to clarify: this solution does not use Chrome's built-in "headless" mode, but instead renders Chrome on a virtual display.Dactylic
Thank you. xvfb-run also works with Selenium without remote debugging options, for example: xvfb-run --server-args="-screen 0 1920x1080x24" python run-selenium.py. See How To Run Your Tests Headlessly with XvfbArose
D
46

EDIT: This answer is no longer correct, see https://mcmap.net/q/273531/-is-it-possible-to-run-google-chrome-in-headless-mode-with-extensions

No, it's not possible, and Chrome developers decided against implementing it in any near future due to complexity of the task.

If you look at that issue you may get the idea that they are still considering it due to ChromeDriver requirements - but instead they decided to make ChromeDriver work without extensions (through DevTools).

Dactylic answered 28/7, 2017 at 11:42 Comment(6)
will headless mode support extension in the future? are there any other web browser that support headless mode with extensions?Odel
Yes firefox supports them. This is a crappy missing feature.Dinosaurian
This answer is not valid anymore: --headless=chrome was added in 2021 as a way of running Chrome in headless mode without loss of features. See the comment here: bugs.chromium.org/p/chromium/issues/detail?id=706008#c36Hex
@MichaelMintz I invite you to make a new answer, would gladly upvote it.Dactylic
That newer answer has already been added by someone else.Hex
have a try "--haedless=new" for chrome version > 109 :)Corollaceous
D
22

Due to recent changes in headless mode, if you are having problems, you might need to use --headless=new instead of --headless=chrome

options.add_argument("--headless=new")

edit: https://www.selenium.dev/blog/2023/headless-is-going-away/

Dah answered 20/1, 2023 at 0:2 Comment(4)
THANKS! How did you get that info?Alderete
@Alderete I dug around and found this bug issue bugs.chromium.org/p/chromium/issues/detail?id=1380881 Now it is added to selenium website too selenium.dev/blog/2023/headless-is-going-awayDah
thanks! "--headless=new" works hereCorollaceous
works fine in 2024Abettor
L
18

You can use pyvirtualdisplay to run the chrome with zero display on your server. The best thing is you can run extensions by using this trick.

Here is my implementation:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
from pyvirtualdisplay import Display

display = Display(visible=0, size=(800, 600))
display.start()

chrome_options = Options()

chrome_options.add_extension("proxy.zip")

driver = webdriver.Chrome(
    executable_path='/usr/bin/chromedriver', 
    chrome_options=chrome_options
)
time.sleep(3)
driver.get("https://ipinfo.io/json")
print(driver.page_source)
driver.close()

display.stop()

You need to install xvfb on your server/machine:

sudo apt install -y xvfb
pip install pyvirtualdisplay

Running it on my AWS Server

aws server

Lightning answered 17/1, 2020 at 19:20 Comment(1)
Just to know, did you use the flags --headless --no-sandbox?Baruch
E
15

Actually it works now, they fixed it. You can use

options.add_argument("--headless=chrome")

Works like a charm. https://bugs.chromium.org/p/chromium/issues/detail?id=706008#c36

Etna answered 22/7, 2022 at 11:41 Comment(2)
this might be a dumb question, but what's options here and how do I use this code snippet to run headless chrome?Variation
@Variation The answer is in python (see john's answer codes above). There 'options' is a variable that stores command line arguments that will be passed when launching any application via command line, in this case the application would be chrome.Catnip
O
4

I have tried it and it works well!

options.add_argument("--headless=new")
Ophthalmoscope answered 27/7, 2023 at 18:4 Comment(1)
I agree, this is the new correct answer. For more information: selenium.dev/blog/2023/headless-is-going-away/…Keane
D
3
options.add_argument("--headless=chrome")

works

Dix answered 11/8, 2022 at 14:7 Comment(1)
Yet it doesn't behave in the same way. On mac, --headless=chrome still creates a chrome instance that shows in the app switched (command-tab), whereas --headless does not. If this is different, what else is?Avogadro
H
2

No, in mode doesn't supports extensions yet.

[email protected] in his comment clearly mentioned:

We've decided against implementing extension support in headless mode for now because supporting all the required features is rather complex

You can find the complete analysis in Add extension support for headless chrome

[email protected] in his comment mentioned the real time issues as:

  1. A lot of extension APIs are specific to non-headless browsers, so can't be supported in headless chrome.
  2. Of the APIs we could feasibly support, only parts are implemented in such a way that we can currently reuse them for headless chrome.
  3. Changing this requires a lot of refactoring that doesn't seem justified given the benefit we'd gain.

He further added,

Either way, extensions would likely have to be adapted to work with headless chrome due to (1). So even if we solved (2), most existing extensions would not be compatible. (This also renders headless chrome unsuitable for testing chrome extensions.)

Most if not all functions that extensions could provide to headless chrome can be realized via the DevTools API that headless exposes to its users. If you have a use case that isn't supported via the DevTools API, please file a feature request.

Moreover, in his comment, [email protected] clearly mentioned:

I realize a lot of folks would like to use extensions with headless but unfortunately that's a large project which we have /no plans to do/. The problem is Headless Chromium is a content embedder which means it doesn't have access to anything from other content embedders such as chrome and unfortunately extensions are a chrome feature.

In another comment he further added, if you're using Selenium through DevTools you can build a proxy. You can filter URLs and modify headers via Network.setRequestInterception and Network.continueInterceptedRequest.

Heliogabalus answered 12/6, 2020 at 15:25 Comment(0)
A
0

I haven't tried it but look at this

https://github.com/php-webdriver/php-webdriver/blob/2ed6645812603b20a7a249d4a66e286eb407300c/lib/Chrome/ChromeOptions.php#L83

phpdoc reads

/**
     * Add a Chrome extension to install on browser startup. Each path should be
     * a packed Chrome extension.
     *
     * @param array $paths
     * @return ChromeOptions
     */
    public function addExtensions(array $paths)
Antemundane answered 31/7, 2021 at 0:12 Comment(0)
S
0

If options.add_argument("--headless=chrome") doesn't work for you, you can use pyvirtualdisplay. First install pyvirtualdisplay with pip and on Linux machine run apt-get install xvfb. You can find here how to use pyvirtualdisplay https://github.com/ponty/PyVirtualDisplay

And you need to remove --headless argument from ChromeOptions.

Slapdash answered 13/1, 2023 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.