9

I am using chromedriver with selenium to download files from application. But while clicking on download button in application, it given error as "Failed-Download error."

Chromedriver version : 2.21 Selenium version : 2.53.0

Code for initializing chrome driver and changing download location :

            String newPath = "D:\\Backup" + File.separator + "Database ";
            new File(newPath).mkdir();
            HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
            chromePrefs.put("profile.default_content_settings.popups", 0);
            chromePrefs.put("download.default_directory", newPath);
            chromePrefs.put("safebrowsing.enabled", "true");
            ChromeOptions options = new ChromeOptions();
            options.setExperimentalOption("prefs", chromePrefs);
            options.addArguments("--test-type");
            DesiredCapabilities cap = DesiredCapabilities.chrome();
            cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            cap.setCapability("disable-popup-blocking", true);
            cap.setCapability(ChromeOptions.CAPABILITY, options);
            System.setProperty("webdriver.chrome.driver", CHROME_DRIVER_PATH);
            driver = new ChromeDriver(cap);
            // Maximize the driver window
            driver.manage().window().maximize();

Error is :

enter image description here

Can someone help me with this? I am able to download file from Chrome manually.

Mosam Mehta
  • 1,658
  • 6
  • 25
  • 34

5 Answers5

14

In my case I had something similar, but the error was in the folder name I used. I describe the path as C:/myFolder instead of C:\myFolder.

In previous version of ChromeDriver the first approach was still valid. Now it looks like this is not working anymore giving some Download error.

Marco smdm
  • 1,020
  • 1
  • 15
  • 25
5

A Failed - Download error is displayed when the provided folder is missing or inaccessible. It could be the case here since I noticed an extra space at the end which is probably stripped once the folder is created. Try this way instead:

String newPath = Path.Combine("D:\\Backup", "Database");
if (!Directory.Exists(newPath)){
    newPath = Directory.CreateDirectory(newPath).FullName;
}
Florent B.
  • 41,537
  • 7
  • 86
  • 101
1

I had a similar problem where i was trying to change the file download directory path at run-time before downloading. Below code worked for me.

//Set Browser Capabilities.
String ProjectDirectory=RunConfiguration.getProjectDir()
String DownloadFolderPath1=ProjectDirectory+"/Downloads"
String DownloadFolderPath=DownloadFolderPath1.replace('/', '\\')
String AppURL=GlobalVariable.MyAppURL
            
Map<String, Object> chromePrefs = new HashMap<String, Object>()
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", DownloadFolderPath)
chromePrefs.put("download.prompt_for_download", false)
chromePrefs.put("plugins.plugins_disabled", "Chrome PDF Viewer");
ChromeOptions options=new ChromeOptions();

//options.addArguments("--headless")
//options.addArguments("--window-size=1920,1080")
options.addArguments("--test-type")
//options.addArguments("--disable-gpu")
options.addArguments("--no-sandbox")
//options.addArguments("--disable-dev-shm-usage")
options.addArguments("--disable-software-rasterizer")
options.addArguments("--disable-popup-blocking")
options.addArguments("--disable-extensions")
options.setExperimentalOption("prefs", chromePrefs)

DesiredCapabilities cap = DesiredCapabilities.chrome()
cap.setCapability(ChromeOptions.CAPABILITY, options)
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true)
System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
WebDriver driver = new ChromeDriver(cap);       
driver.get(AppURL)
driver.manage().window().maximize()
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
Adarsh
  • 73
  • 10
1

It took some time to find out my error, but it is very simple to fix. In my case, I should not use relative filepath names for the download folder, instead, I had to use absolute filepath names.

Matheus Araujo
  • 5,551
  • 2
  • 22
  • 23
0

If you are on Linux and "download.default_directory" still hold your personal directory value, watch env XDG_DOWNLOAD_DIR in file ~/.config/user-dirs.dirs

You can remove variable form that file or, you can set it to whatever you like before running your program.

My SW set:

  • Ubuntu bionic, 18.04.5 LTS
  • chromedriver.86.0.4240.22.lin64
  • Python 3.9
  • selenium 3.141.0
  • splinter 0.14.0
Mintaka
  • 89
  • 1
  • 3