How to set Download Directory Path in Chrome & Firefox

DownloadDirectory

Set download directory

Supported on:

Description:

How to implement it on Selenium:

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:

prefs.put("download.prompt_for_download", false);
prefs.put("download.default_directory", downloadFilepath);
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);
profile.setPreference("browser.download.folderList", 2)
profile.setPreference("browser.download.manager.showWhenStarting", False)
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/csv, text/csv, text/plain,application/octet-stream doc xls pdf txt");

Note for Google Chrome OS devices supporting Android apps:

Reference:

Geek Culture

Proud to geek out. Follow to join our +1.5M monthly readers.