如何使用 chrome 驱动程序禁用所有 chrome 扩展

How to disable ALL chrome extensions using chromedriver

升级到 chromedriver 74 后,注意到 Windows 上的奇怪扩展行为。 是否可以关闭所有扩展程序?

  1. 启动 chromedriver
chromedriver --log-level=ALL
  1. 创建禁用扩展程序的会话
curl -d '{"desiredCapabilities":{"browserName":"chrome","goog:chromeOptions":{"args":["--disable-extensions"]}}}' http://localhost:9515/session

加载了一些开发工具扩展

[1558606783.990][INFO]: Launching chrome: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-extensions --disable-extensions-except="C:\Users\user\AppData\Local\Temp\scoped_dir19964_411\internal" --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --force-fieldtrials=SiteIsolationExtensions/Control --ignore-certificate-errors --log-level=0 --no-first-run --password-store=basic --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir="C:\Users\user\AppData\Local\Temp\scoped_dir19964_22650" data:,

备注

--disable-extensions-except="C:\Users\user\AppData\Local\Temp\scoped_dir19964_411\internal"

有没有办法摆脱它?在 chromedriver 文档中没有找到任何线索,这些线索非常粗略。

TL;DR

chromeOptions.useAutomationExtension 设置为 false,它将阻止注入 Chrome 自动化扩展

{
  "desiredCapabilities": {
    "browserName": "chrome",
    "goog:chromeOptions": {
      "useAutomationExtension": false,
      "args": [
        "--disable-extensions"
      ]
    }
  }
}

长版

当前版本 (75.0.)chromedriver 文档 http://chromedriver.chromium.org/capabilities, but can be traced in source code 中未提及自动化扩展标志

    parser_map["useAutomationExtension"] =
        base::Bind(&ParseBoolean, &capabilities->use_automation_extension);
    status = internal::ProcessExtensions(
        capabilities.extensions, extension_dir->GetPath(),
        capabilities.use_automation_extension, &switches, extension_bg_pages);
  if (include_automation_extension) {
    ...
    if (switches->HasSwitch("disable-extensions")) {
      UpdateExtensionSwitch(switches, "disable-extensions-except",
                            automation_extension.value());

54594305 Java 中所述,使用 selenium 驱动程序的代码将是

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);