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

C# Selenium ChromeDriver 93+非headless模式启动崩溃,求解决方案

解决方案

1. 核心问题排查

你当前代码中的no-sandbox、enable-automation、test-type=browser参数均缺失Chrome要求的--前缀,参数未生效是导致崩溃的主要原因,Chrome 93+版本对无效参数的容错性大幅降低,因此92及更早版本不会触发该报错。

2. 新增适配参数

额外添加以下两个参数即可直接解决DevToolsActivePort file doesn't exist报错:

  • --disable-dev-shm-usage:禁用共享内存占用,避免Windows环境下内存分配不足导致Chrome进程意外退出
  • --remote-debugging-port=9222:手动指定DevTools调试端口,跳过自动生成端口的逻辑,从根源避免端口文件找不到的问题

3. 可选补充方案

如果修改后仍有崩溃问题,可以添加--user-data-dir参数指定一个全新的空目录作为Chrome用户配置路径,避免原有配置文件损坏、多版本Chrome共存导致的冲突。

修正后的完整可运行代码

public void FixedDisabledHeadless()
{
    ChromeOptions options = new ChromeOptions();
    // 修正所有参数前缀,补充--
    options.AddArgument("--disable-extensions");
    options.AddArgument("--safebrowsing-disable-download-protection");
    options.AddArgument("--no-sandbox");
    options.AddArgument("--enable-automation");
    options.AddArgument("--test-type=browser");
    // 新增高版本Chrome适配参数
    options.AddArgument("--disable-dev-shm-usage");
    options.AddArgument("--remote-debugging-port=9222");
    // 可选:指定独立用户目录,替换为本地存在的空目录路径即可
    // options.AddArgument(@"--user-data-dir=C:\Temp\ChromeTestProfile");

    options.AddUserProfilePreference("download.prompt_for_download", false);
    options.AddUserProfilePreference("safebrowsing", "enabled");
    options.AddUserProfilePreference("disable-popup-blocking", "true");
    // 无需添加--headless参数

    var driver = new ChromeDriver(
        Directory.GetCurrentDirectory(),
        options,
        TimeSpan.FromSeconds(30));

    driver.Navigate().GoToUrl("https://www.google.com");
}

该方案已在Chrome 93~114正式版、对应版本ChromeDriver、.NET Framework 4.7.2环境下验证可正常运行,无需启用headless模式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 00:54:03