是否可以在带扩展的无头模式下 运行 Google Chrome ?

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

我无法使用无头模式在 Google Chrome 中使用我当前安装的扩展。有什么方法可以启用它们吗?

检查扩展是否有效的一种简单方法是添加,例如,“Comic Sans Everything”扩展。

所以 Google 看起来像这样:

但是,如果我使用无头模式(google-chrome --headless --disable-gpu --screenshot https://www.google.com)对页面进行截图,结果是:

不,这是不可能的,并且 Chrome 开发人员 decided against implementing it in any near future 由于任务的复杂性。

如果您查看该问题,您可能会认为由于 Chrome 驱动程序要求,他们仍在考虑它 - 但他们决定让 Chrome 驱动程序在没有扩展的情况下工作(通过 DevTools ).

您可以 运行 Chrome 使用 Xvfb 无头扩展。

  1. 安装 Xvfb。在 Fedora 上 sudo dnf install xorg-x11-server-Xvfb
  2. xvfb-run google-chrome --remote-debugging-port=9222 --disable-gpu https://www.google.com
  3. 使用 chrome-remote-interface(或另一个 Chrome 调试协议客户端)触发屏幕截图。

更复杂,但确实有效。这是我们用于无头 chrome 扩展测试的东西。

您可以使用 pyvirtualdisplay 到 运行 chrome 在您的服务器上显示为零。最好的事情是你可以使用这个技巧 运行 扩展。

这是我的实现:

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()

您需要在 server/machine 上安装 xvfb

sudo apt install -y xvfb
pip install pyvirtualdisplay

运行 它在我的 AWS 服务器上

in 模式尚不支持扩展。

skyos...@chromium.org在他的comment中明确提到:

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

您可以在 Add extension support for headless chrome

中找到完整的分析

eseckler@chromium.org 在他的 comment 中提到了实时问题:

  1. 许多扩展 API 是特定于非无头浏览器的,因此无法在无头浏览器中得到支持chrome。
  2. 在我们可以切实支持的 API 中,只有部分实现方式是我们目前可以将它们重用于无头 chrome。
  3. 更改此设置需要大量重构,考虑到我们将获得的好处,这似乎不合理。

他进一步补充道,

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.

而且,在他的comment中,alexclarke@chromium.org明确提到:

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.

在另一个comment he further added, if you're using through you can build a proxy. You can filter URLs and modify headers via Network.setRequestInterception and Network.continueInterceptedRequest.

我没试过,但看看这个

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

phpdoc 读取

/**
     * 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)