Reputation: 4675
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:
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:
Upvotes: 72
Views: 63108
Reputation: 151
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
Upvotes: 15
Reputation: 51
I have tried it and it works well!
options.add_argument("--headless=new")
Upvotes: 5
Reputation: 221
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/
Upvotes: 22
Reputation: 1
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.
Upvotes: 0
Reputation: 77492
EDIT: This answer is no longer correct, see https://stackoverflow.com/a/73079789/934239
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).
Upvotes: 46
Reputation: 2544
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
Upvotes: 18
Reputation: 14931
I haven't tried it but look at this
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)
Upvotes: 0
Reputation: 193088
No, google-chrome in headless 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:
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.
Upvotes: 2
Reputation: 5720
You can run Chrome with extensions headless using Xvfb.
sudo dnf install xorg-x11-server-Xvfb
xvfb-run google-chrome --remote-debugging-port=9222 --disable-gpu https://www.google.com
More complicated, but it does work. It's what we use for headless chrome extension testing.
Upvotes: 48