更新到最新的 Chrome 版本后,PDF 的自动下载功能在隐身模式下不再有效。相反,将显示 Windows 另存为文件弹出窗口

After updating to the latest Chrome version, the auto-download of PDFs no longer works in Incognito mode.Instead, a Windows SaveAs File Popup appears

提问人:Chandler 提问时间:9/21/2023 更新时间:11/8/2023 访问量:282

问:

Chrome 版本:版本 117.0.5938.89(官方版本)(64 位)

自动化测试设置:

我正在使用 Selenium WebDriver 和 C# 进行自动化测试。

问题描述:

在隐身模式下,我曾经能够在单击按钮时自动下载 PDF 并在测试中执行操作。但是,在升级到最新的 Chrome 版本后,当我尝试在单击按钮时下载 PDF 时,隐身模式下的相同驱动程序初始化方法现在会触发 Windows 另存为弹出窗口。我试图通过设置默认下载目录来解决这个问题,但问题仍然存在。

请求协助:

我需要帮助来避免 Windows 弹出窗口并启用没有另存为弹出窗口的直接文件下载。

驱动程序初始化方法:

下面是驱动程序初始化方法的代码

 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;
}
C# Google-chrome 硒-webdriver 隐身模式

评论


答:

0赞 whopacha 11/8/2023 #1

我有同样的问题(我自己没有使用隐身) 我已经有一些这些选项,但添加了一些我看到的选项,它修复了它。

请尝试以下选项:

        // Profile options for downloading PDF's
        co.AddUserProfilePreference("download.default_directory", this.downloadDir);
        co.AddUserProfilePreference("savefile.default_directory", this.downloadDir);
        co.AddUserProfilePreference("download.prompt_for_download", false);
        co.AddUserProfilePreference("download.directory_upgrade", true);
        ////co.AddUserProfilePreference("download.extensions_to_open", "pdf");
        co.AddUserProfilePreference("safebrowsing.enabled", true);
        co.AddUserProfilePreference("plugins.plugins_disabled", "Chrome PDF Viewer");
        co.AddUserProfilePreference("plugins.always_open_pdf_externally", true);
        co.AddUserProfilePreference("disable-popup-blocking", true);
        co.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 1);