1

We are trying to verify the downloaded file in headless mode with C#, the file is downloading but not saving in the download path, so my test script is failing.

Any idea for this?

The code is below

ChromeOptions option = new ChromeOptions(); 
option.AddUserProfilePreference("download.default_directory", @"%USERPROFILE%\Downloads"); 
option.AddArgument("--headless"); 
option.AddArgument("--window-size=1920,1080"); 
option.AddArguments("--disable-web-security"); 
option.AddArguments("--allow-running-insecure-content"); 
driver = new ChromeDriver(option);
2
  • Hi put the script you used to download and verify the file
    – PDHide
    Commented Oct 31, 2019 at 7:00
  • Did you find any solution for this?
    – ChathuD
    Commented Oct 13, 2020 at 6:56

1 Answer 1

1

I had a similar problem recently I had to add extra options to set the download path for headless.

    var options = new ChromeOptions();
    options.AddArgument("--headless");
    options.AddArgument("--disable-extensions");
    options.AddArgument("--disable-gpu");
    options.AddArgument("--disable-dev-shm-usage");
    options.AddArgument("--no-sandbox");
    options.AddArgument("--ignore-certificate-errors");
    options.AddUserProfilePreference("download.default_directory", Path.GetTempPath());
    options.AddUserProfilePreference("download.directory_upgrade", true);
    options.AddUserProfilePreference("download.prompt_for_download", false);

    var driver = new ChromeDriver(options);

    var settingForHeadlessDownloads = new Dictionary<string, object>
    {
        { "behavior", "allow" },
        { "downloadPath", Path.GetTempPath() }
    };
    driver.ExecuteChromeCommand("Page.setDownloadBehavior", settingForHeadlessDownloads);

This answers has some more details, apperently it is a security feature, but is for Python. https://stackoverflow.com/questions/45631715/downloading-with-chrome-headless-and-selenium

5
  • we tried this already ,still no luck with chrome version 78. Commented Oct 31, 2019 at 12:24
  • ChromeOptions option = new ChromeOptions(); option.AddUserProfilePreference("download.default_directory", @"%USERPROFILE%\Downloads"); option.AddArgument("--headless"); option.AddArgument("--window-size=1920,1080"); option.AddArguments("--disable-web-security"); option.AddArguments("--allow-running-insecure-content"); driver = new ChromeDriver(option); Commented Oct 31, 2019 at 12:26
  • What is that last comment? Commented Oct 31, 2019 at 12:29
  • we tried above code along with the shared script,but not sure where we are failing Commented Oct 31, 2019 at 12:39
  • Added my full options code aswell. I am on chrome v77 and cannot upgrade soon to test if it still works. Commented Oct 31, 2019 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.