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

Chrome 117隐身模式下Selenium自动下载PDF弹出保存窗口求助

问题详情

环境信息

  • Chrome版本:Version 117.0.5938.89 (官方版本) (64位)
  • 自动化测试环境:使用Selenium WebDriver结合C#进行自动化测试

问题描述

此前在隐身模式下,点击按钮即可自动下载PDF并执行后续测试操作,但升级至上述Chrome版本后,相同的驱动初始化逻辑在隐身模式下点击按钮下载PDF时,会触发Windows的“另存为”文件弹窗。已尝试设置默认下载目录,但问题仍未解决。

求助需求

实现无需弹出“另存为”窗口,直接完成PDF文件下载。

当前驱动初始化代码

private static IWebDriver GetChromeDriver(string userAgent)
{
    var options = new ChromeOptions();
    options.AddArgument("incognito");
    options.AddArguments("disable-infobars");
    // options.AddArguments("--headless");

    // Session 0 limit - 1024 x 768
    options.AddArguments("--window-size=1024,768");
    options.AddArguments("--window-position=0,0");
    options.AddArgument($"--user-agent= {userAgent}");

    // To disable PDF viewer plugins
    options.AddUserProfilePreference("plugins.always_open_pdf_externally", true);
    options.AddUserProfilePreference("download.prompt_for_download", false);

    if (userAgent.Contains("Mobile"))
    {
        // Commenting "EnableMobileEmulation" with Device Name - as it overrides the set Driver's userAgent value
        // Added Device Settings to pass userAgent and explicit values of Mobile Device - To avoid captcha block
        var settings = new ChromeMobileEmulationDeviceSettings(userAgent)
        {
            Height = 731,
            Width = 411,
            PixelRatio = 2.6
        };
        options.EnableMobileEmulation(settings);

        // options.EnableMobileEmulation("Pixel 2");
    }
    options.AddArgument("--disable-backgrounding-occluded-windows");

    // options.BinaryLocation = @"C:\Program Files\Google\Chrome\Application\chrome.exe";

    var driver = new ChromeDriver(Directory.GetCurrentDirectory(), options, TimeSpan.FromMinutes(3));
    return driver;
}

解决方案

针对Chrome 117+版本隐身模式下的PDF下载弹窗问题,可尝试以下调整:

  1. 补充指定默认下载目录
    隐身模式下会隔离本地用户配置,仅禁用下载弹窗的设置可能不生效,需明确指定下载路径:

    string downloadPath = @"C:\Your\Custom\Download\Path";
    options.AddUserProfilePreference("download.default_directory", downloadPath);
    options.AddUserProfilePreference("download.directory_upgrade", true);
    
  2. 新增PDF强制下载的配置项
    Chrome新版本对PDF预览逻辑有调整,添加以下配置强制跳过预览直接下载:

    options.AddUserProfilePreference("plugins.plugins_list", new List<object> { new { name = "Chrome PDF Viewer" } });
    options.AddUserProfilePreference("download.extensions_to_open", "");
    options.AddArgument("--disable-features=PDFViewer");
    
  3. 调整后的完整初始化代码
    整合所有修复项后的代码示例:

    private static IWebDriver GetChromeDriver(string userAgent)
    {
        var options = new ChromeOptions();
        options.AddArgument("incognito");
        options.AddArguments("disable-infobars");
        // options.AddArguments("--headless");
    
        // Session 0 limit - 1024 x 768
        options.AddArguments("--window-size=1024,768");
        options.AddArguments("--window-position=0,0");
        options.AddArgument($"--user-agent= {userAgent}");
    
        // 下载核心配置
        string downloadPath = @"C:\Your\Custom\Download\Path";
        options.AddUserProfilePreference("download.default_directory", downloadPath);
        options.AddUserProfilePreference("download.prompt_for_download", false);
        options.AddUserProfilePreference("download.directory_upgrade", true);
    
        // PDF强制下载配置
        options.AddUserProfilePreference("plugins.always_open_pdf_externally", true);
        options.AddUserProfilePreference("plugins.plugins_list", new List<object> { new { name = "Chrome PDF Viewer" } });
        options.AddUserProfilePreference("download.extensions_to_open", "");
        options.AddArgument("--disable-features=PDFViewer");
    
        if (userAgent.Contains("Mobile"))
        {
            var settings = new ChromeMobileEmulationDeviceSettings(userAgent)
            {
                Height = 731,
                Width = 411,
                PixelRatio = 2.6
            };
            options.EnableMobileEmulation(settings);
        }
        options.AddArgument("--disable-backgrounding-occluded-windows");
    
        var driver = new ChromeDriver(Directory.GetCurrentDirectory(), options, TimeSpan.FromMinutes(3));
        return driver;
    }
    
  4. 验证驱动版本兼容性
    确保ChromeDriver版本与Chrome 117.0.5938.89完全匹配,版本不匹配会导致配置失效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 06:22:38