How to set Download Directory Path in Chrome & Firefox & Edge
DownloadDirectory
How to implement it on Selenium:
First Setting & Creating directory if not available:
String parentDirectoryPath = System.getProperty("user.dir");
String downloadFilepath = parentDirectoryPath+"/downloads/";
APP_LOGS.debug("Chrome Download path set to: "+downloadFilepath);
File downloadFolder = new File(downloadFilepath);
if (!downloadFolder.exists()){
downloadFolder.mkdir();
}
In Chrome:
We have to set the preferences of the browser and pass the download.default_directory parameter. We also need to mention the path of the download directory with the above parameter. This preference is sent to the ChromeOptions object with the add_experimental_option method (As shown below).
prefs.put("download.prompt_for_download", false);
prefs.put("download.default_directory", downloadFilepath);
Complete Code:
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadFilepath);
prefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
In Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2)
profile.setPreference("browser.download.manager.showWhenStarting", False)
profile.setPreference("browser.download.dir", downloadFilepath)
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/csv, text/csv, text/plain,application/octet-stream doc xls pdf txt");
FirefoxOptions option = new FirefoxOptions();
option.setProfile(profile);
WebDriver driver = new FirefoxDriver(option);
browser.download.folderList:
profile.setPreference("browser.download.folderList", 2)
0
means to download to the desktop.1
means to download to the default.2
means to use the directory we have specified inbrowser.download.dir
path.
browser.download.manager.showWhenStarting:
profile.setPreference("browser.download.manager.showWhenStarting", False)
This is to specify whether or not the Download Manager window is displayed when a file download is initiated. Setting it to “false” means that download prompt will not be shown.
browser.helperApps.neverAsk.saveToDisk:
This will not ask for the file type given. A comma-separated list of MIME types to save to disk without asking what to use to open the file. Default is an empty string.
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/csv, text/csv, text/plain,application/octet-stream doc xls pdf txt");
In EDGE:
String parentDirectoryPath = System.getProperty("user.dir");
String downloadFilepath = parentDirectoryPath+"/downloads/";
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", downloadFilepath);
EdgeOptions options = new EdgeOptions();
options.setExperimentalOption("prefs", prefs);