Software Development

How can I automate downloading and saving files to a specific local directory using Selenium WebDriver?

SA Asked by Sarah Jenkins · 14-05-2024
0 upvotes 14,263 views 0 comments
The question

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

0
AM
Answered on 15-05-2024

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.

0
MI
Answered on 20-05-2024

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.

DA 21-05-2024

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.

0
RO
Answered on 02-06-2024

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.

SA 03-06-2025

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.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session