1

I'm using the 77th version of chrome to test some downloads. But I don't understand why it doesn't let download files on headless mode (Only happens on headless mode). This is the code I'm using.

_chromeOptions.AddUserProfilePreference("download.default_directory", @"Directory Folder"); _chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl"); _chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true"); _webdriver = new ChromeDriver(_chromeOptions);

4 Answers4

4

I was able to download files in headless mode using the following ChromeOptions :

            var chromeOptions = new ChromeOptions();
            chromeOptions.AddArgument("--headless");
            chromeOptions.AddArgument("--disable-gpu");
            chromeOptions.AddUserProfilePreference("download.default_directory", ApplicationSettings.StagingDirectory);
            chromeOptions.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 1);
            driver = new ChromeDriver(chromeOptions);

Chrome Version - 89.0.4389

Chrome Driver Version - 89.0.4389.2300

Daniel Offner
  • 204
  • 2
  • 3
1

This function returns headless Chrome browser instance with automatic download set to "USERPROFILE" Downloads folder. You can hardcode the download folder you wish.

Call function GetBrowserWebDriver("Chrome") from the test initializer

public IWebDriver GetBrowserWebDriver(string browser)
        {
            IWebDriver currentDriver = null;        
            switch (browser)
            {
                case "Chrome":                    
                    var options = new ChromeOptions();
                    options.AddArgument("headless");
                    string downloadPath = System.Environment.GetEnvironmentVariable("USERPROFILE") + "\\Downloads";
                    options.AddUserProfilePreference("download.default_directory", downloadPath);
                    options.AddUserProfilePreference("profile.default_content_setting_values.automatic_downloads", 1);
                    options.AddArgument("--window-size=1920,1080");                    
                    currentDriver = new ChromeDriver(options);
                    break;
                case "Firefox":
                    currentDriver = new FirefoxDriver();
                    break;
                case "IE":
                    currentDriver = new InternetExplorerDriver(new InternetExplorerOptions() { IgnoreZoomLevel = true });
                    break;
                default:
                    throw new NotSupportedException("");
            }
            return currentDriver;
        }
Avinash
  • 188
  • 5
0

As alternative, you can download files using Firefox headless browser.

FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Windows\\temp");
profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
options.setProfile(profile);
driver = new FirefoxDriver(options);
Mert Köklü
  • 2,183
  • 2
  • 16
  • 20
0

Downloading of files is disabled in Chrome Headless mode by default. See: https://bugs.chromium.org/p/chromium/issues/detail?id=696481

You need to make an API call to the driver to enable it.

var driver = new ChromeDriver(driverService, options);
// Allow download in headless mode
var param = new Dictionary<string, string> {{"behavior", "allow"}, {"downloadPath", DownloadPath}
};
var cmdParam = new Dictionary<string, object> {{"cmd", "Page.setDownloadBehavior"}, {"params", param}};
var url = driverService.ServiceUrl + "session/" + driver.SessionId + "/chromium/send_command";
var cli = new WebClient {Headers = {[HttpRequestHeader.ContentType] = "application/json"}};
_ = cli.UploadString(url, JsonConvert.SerializeObject(cmdParam));
J.D. Cain
  • 639
  • 4
  • 16