I’m currently working on a test automation suite using Selenium WebDriver and I'm stuck on a specific task. I need to trigger a file download from a web portal and ensure the file is saved to a custom local directory rather than the default downloads folder. Is there a reliable way to configure browser preferences or use a specific class to handle this across Chrome and Firefox without manual intervention?
3 answers
To achieve this in Selenium, you need to manipulate the browser's profile settings before launching the driver instance. For Chrome, you should use ChromeOptions and set the prefs hash map, specifically defining download.default_directory to your desired path. It is also crucial to disable the download prompt by setting download.prompt_for_download to false. For Firefox, the process is similar but uses FirefoxProfile to adjust the browser.download.dir and browser.download.folderList properties. This ensures your automation script remains headless-friendly and completely hands-off.
Does your current framework handle the dynamic naming of files once they are saved, or are you strictly looking for a way to set the path? I've found that even if the directory is set correctly, identifying the most recently downloaded file for validation can be quite a headache in parallel execution environments.
You should look into using ChromeOptions for this. Just pass a Map with the key download.default_directory set to your target path string during the driver initialization phase.
I agree with Robert. I used this exact method last week in a CI/CD pipeline and it worked perfectly for PDF exports. Just make sure the path is absolute to avoid any permission errors during execution.
Michael, that is a great point! To handle that, most people implement a file watcher or a utility method that checks the directory for the newest file extension after the download click. You can use Java’s File class or Python’s os module to poll the folder until the .crdownload or .part temporary file disappears, confirming the transfer is 100% complete and ready for your assertions.