You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome 137版本下Selenium C#无法安装本地扩展的解决方案咨询

Chrome 137.0.7151.56 下 Selenium C# 加载扩展失效的解决方案

问题背景

此前Chrome 136.*版本中,通过Selenium C#加载自制CRX扩展或未打包扩展均正常,但升级至Chrome 137.0.7151.56正式版后,两种加载方式均失效,仅Edge浏览器可正常安装该扩展。

当前CRX加载代码:

ChromeOptions options = new ChromeOptions();
options.AddExtension(credential.myExtensionPathOfCRXFile);

var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

var driver = new ChromeDriver(driverService, options);

自制CRX生成逻辑(仅简单打包ZIP):

public static class ExtensionHelper
{
    public static void CreateCrx(string extensionFolder, string crxOutputPath)
    {
        string manifestPath = Path.Combine(extensionFolder, "manifest.json");
        if (!File.Exists(manifestPath))
        {
            Console.WriteLine("Error: Manifest file not found.");
            return;
        }

        using (FileStream crxStream = new FileStream(crxOutputPath, FileMode.Create))
        using (ZipArchive crxArchive = new ZipArchive(crxStream, ZipArchiveMode.Create))
        {
            crxArchive.CreateEntryFromFile(manifestPath, "manifest.json");
            foreach (var file in Directory.GetFiles(extensionFolder))
            {
                if (Path.GetFileName(file) != "manifest.json")
                {
                    crxArchive.CreateEntryFromFile(file, Path.GetFileName(file));
                }
            }
        }
    }
}

核心原因分析

  1. CRX格式不规范:Chrome 137强化了扩展签名验证,当前生成的CRX只是普通ZIP文件重命名,缺少CRX格式要求的签名头结构,被Chrome拦截。
  2. 未打包扩展加载限制:Chrome 137对自动化环境加载未打包扩展新增权限限制,单纯使用--load-extension参数已无法绕过。

解决方案

方案1:调整参数加载未打包扩展

添加额外参数允许自动化环境加载未打包扩展,确保使用绝对路径:

ChromeOptions options = new ChromeOptions();
// 指定仅加载目标扩展,排除其他扩展
options.AddArguments($"--disable-extensions-except={Path.GetFullPath(extensionFolder)}");
// 加载未打包扩展
options.AddArguments($"--load-extension={Path.GetFullPath(extensionFolder)}");
// 测试环境可添加以下参数绕过沙箱限制
options.AddArguments("--no-sandbox");
options.AddArguments("--disable-dev-shm-usage");

var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

var driver = new ChromeDriver(driverService, options);

方案2:使用Chrome DevTools Protocol (CDP) 安装扩展

通过CDP命令直接安装未打包扩展,绕过Chrome加载限制:

ChromeOptions options = new ChromeOptions();
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;

var driver = new ChromeDriver(driverService, options);
// 创建CDP会话
var cdpSession = driver.CreateDevToolsSession();
// 安装未打包扩展
cdpSession.SendCommand("Browser.installExtension", new Dictionary<string, object>
{
    {"extensionPath", Path.GetFullPath(extensionFolder)},
    {"allowFileAccess", true}
});

方案3:生成规范带签名的CRX文件

若必须使用CRX格式,需生成符合Chrome标准的带签名CRX:

  • 使用Chrome自带打包功能:打开chrome://extensions/,开启开发者模式,点击「打包扩展程序」选择扩展文件夹,生成带签名的CRX和PEM密钥文件。
  • 替换现有ExtensionHelper逻辑,实现CRX签名头生成(需RSA签名逻辑,或使用第三方库如CrxPacker)。

方案4:临时降级Chrome版本

若需快速恢复功能,可降级Chrome至136.*版本,并确保ChromeDriver版本与Chrome版本匹配。

内容的提问来源于stack exchange,提问作者Zubair Khakwani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 01:18:28