πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
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 + Java
import 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);