Automating File Downloads using Selenium Python
Automating file downloads is a common task in web testing and browser automation. Whether it’s downloading reports, images, PDFs, or spreadsheets, Selenium with Python offers a powerful and flexible way to automate these actions. However, since Selenium interacts with web elements and doesn’t natively handle OS-level popups like the browser’s file download dialog, it requires additional setup to automate file downloads successfully.
In this blog, we’ll walk through how to automate file downloads using Selenium in Python, including configuring the browser, handling download dialogs, and verifying file download success.
Why File Downloads Are Tricky with Selenium
Selenium controls the browser through the DOM (Document Object Model), but file download dialogs are not part of the DOM. These are system-level pop-ups that Selenium cannot control directly. That’s why we need to configure browser preferences to automatically download files to a specific location without prompting.
Step-by-Step: Automating File Downloads in Chrome
To automate file downloads, you need to:
- Set the download directory
- Disable download popups
- Specify file types to download automatically
Here's how you can do it using Google Chrome and Selenium with Python:
python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time
import os
# Define download directory
download_dir = "/path/to/download/folder"
# Set Chrome options
chrome_options = Options()
prefs = {
"download.default_directory": download_dir,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
}
chrome_options.add_experimental_option("prefs", prefs)
# Set up WebDriver
service = Service(executable_path="path/to/chromedriver")
driver = webdriver.Chrome(service=service, options=chrome_options)
# Navigate to file download page
driver.get("https://www.sample-videos.com/download-sample-pdf.php")
# Click on download link
driver.find_element("xpath", "//a[contains(text(),'Download Sample pdf File')]").click()
# Wait for download to complete
time.sleep(5) # Or use a better wait strategy
# Check if file exists
filename = os.path.join(download_dir, "sample.pdf")
if os.path.exists(filename):
print("Download successful!")
else:
print("Download failed.")
driver.quit()
Automating File Downloads in Firefox
Firefox also allows you to configure download preferences using FirefoxProfile.
python
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.dir", "/path/to/download/folder")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")
profile.set_preference("pdfjs.disabled", True)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://file-examples.com/index.php/sample-documents-download/sample-pdf-download/")
driver.find_element("xpath", "//a[text()='Download sample pdf file']").click()
Tips and Best Practices
- Always configure the download directory dynamically using Python’s tempfile or os modules for testing environments.
- Use proper MIME types for the file format you're downloading. For example, PDF: application/pdf, Excel: application/vnd.ms-excel.
- Avoid using fixed sleep timers; instead, use logic to check if the file has been fully downloaded.
- Use os.path.exists() or file size monitoring to verify download completion.
- Consider headless mode if running in CI/CD pipelines.
Conclusion
Automating file downloads using Selenium in Python involves more than just clicking a download button. It requires smart browser configuration to bypass popups and save files automatically. With the right setup, you can streamline your test flows, validate download functionality, and make your automation more robust.
Whether you're downloading PDFs, CSVs, or reports, this knowledge will save you time and make your Selenium tests far more effective.
Learn Selenium with Pyhton Training Hyderabad
Read More: Handling Dynamic Elements in Selenium Python
Visit IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment