Not able to verify the downloaded file with Chrome WebDriver running headless



  • 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);
    


  • 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



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2