Back to All Scenarios
Scenario 43 of 100
File Upload/Download
Advanced
Downloading Files to a Specific Target Directory
πScenario Overview
Downloading Files to a Specific Target Directory
Key Takeaways & Cheat Sheet
- βDefine a custom download path directory folder in your project workspace
- βConfigure ChromeOptions preferences dictionary settings
- βDisable download confirmation popups using preferences parameters
- βPass the custom ChromeOptions configuration into your ChromeDriver initialization
Short Direct Answer
To control download locations, customize your browser driver profile at startup. For Chrome, define preferences in `ChromeOptions` (like `download.default_directory` and disabling confirmation prompts) to force downloads directly into a specific folder inside your project workspace.
β οΈ Senior Warning (Red Flag)
Do not use default download paths. Default paths depend on the OS and local machine logins, which will fail when executed on remote Jenkins pipelines.
π‘ STAR Deep Dive Explanation & Pro Tip
Using user.dir ensures that your paths are dynamic and relative to the project root, keeping your suite portable across different machines and servers.
SeleniumAutomation.java
Selenium 4 + Javaimport org.openqa.selenium.chrome.ChromeOptions;
import java.util.HashMap;
import java.util.Map;
// β
Define custom workspace download folder absolute path
String downloadPath = System.getProperty("user.dir") + "\target\downloads";
// β
Set Chrome Options configuration
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadPath);
prefs.put("download.prompt_for_download", false);
prefs.put("plugins.always_open_pdf_externally", true); // Auto-download PDFs
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);