Selenium does not wait for a download to finish. Implement a loop to check if the file exists in the directory before proceeding with the next test step.
Changing the default download directory in Selenium with Java is a common requirement for automating file-related workflows. By default, browsers save files to the system's "Downloads" folder, but you can override this behavior using browser-specific options.
Chrome uses a key-value map called prefs to handle browser behaviors. To change the download directory, you must set the download.default_directory capability. Store your target folder path in a string. change download folder selenium java
Selenium will not create the folder for you. Use new File(downloadPath).mkdirs(); in Java before launching the driver.
import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import java.util.HashMap; import java.util.Map; public class ChromeDownloadTest { public static void main(String[] args) { String downloadPath = "C:\\CustomDownloadFolder"; HashMap chromePrefs = new HashMap<>(); chromePrefs.put("profile.default_content_settings.popups", 0); chromePrefs.put("download.default_directory", downloadPath); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("prefs", chromePrefs); ChromeDriver driver = new ChromeDriver(options); // Navigate and click download... } } Use code with caution. Firefox: Using FirefoxProfile Selenium does not wait for a download to finish
📍 If you are running tests on a Remote Grid or Docker , setting a local path won't work. You will need to download the file to the remote node and use a file-sharing mechanism to retrieve it. To help you get this working perfectly, tell me:
browser.helperApps.neverAsk.saveToDisk : A comma-separated list of MIME types to download without a pop-up. By default, browsers save files to the system's
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; public class FirefoxDownloadTest { public static void main(String[] args) { String downloadPath = "C:\\CustomDownloadFolder"; FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("browser.download.dir", downloadPath); profile.setPreference("browser.download.folderList", 2); // This avoids the "Open or Save" dialog for PDFs and ZIPs profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf,application/zip,application/octet-stream"); FirefoxOptions options = new FirefoxOptions(); options.setProfile(profile); FirefoxDriver driver = new FirefoxDriver(options); } } Use code with caution. Microsoft Edge: Using EdgeOptions