How to Download Files Using Selenium WebDriver
To download a file using
Selenium, configure the browser's download settings to save files automatically to a chosen folder. Then, trigger the download by clicking the download link or button using WebDriver. This avoids manual popups and lets Selenium handle file downloads smoothly.Syntax
Set browser preferences to specify the download folder and disable download popups. Then use Selenium commands to click the download link.
options: Browser settings to control download behavior.driver.get(): Opens the page with the file link.driver.find_element(): Locates the download link/button.element.click(): Starts the file download.
python
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_experimental_option('prefs', { 'download.default_directory': '/path/to/download/folder', 'download.prompt_for_download': False, 'download.directory_upgrade': True, 'safebrowsing.enabled': True }) driver = webdriver.Chrome(options=options) driver.get('https://example.com/download-page') download_button = driver.find_element('xpath', '//a[@id="download"]') download_button.click()
Example
This example shows how to set Chrome options to download a PDF file automatically to a folder without popup dialogs. It opens a sample page and clicks the download link.
python
from selenium import webdriver from selenium.webdriver.chrome.options import Options import time # Set download folder path download_folder = '/tmp/selenium_downloads' # Configure Chrome options options = Options() options.add_experimental_option('prefs', { 'download.default_directory': download_folder, 'download.prompt_for_download': False, 'download.directory_upgrade': True, 'safebrowsing.enabled': True }) # Start Chrome with options driver = webdriver.Chrome(options=options) # Open page with file to download driver.get('https://file-examples.com/index.php/sample-documents-download/sample-pdf-download/') # Find and click the first download button download_link = driver.find_element('xpath', '(//a[text()="Download sample pdf file"])[1]') download_link.click() # Wait for download to complete (simple wait) time.sleep(5) driver.quit()
Output
No visible output; file is downloaded to /tmp/selenium_downloads folder.
Common Pitfalls
Common mistakes when downloading files with Selenium include:
- Not setting the browser's download directory, causing files to save in default locations.
- Not disabling download prompts, which block automation.
- Using incorrect locators for the download button/link.
- Not waiting enough time for the download to finish before closing the browser.
Always verify the download folder and use explicit waits if needed.
python
from selenium import webdriver from selenium.webdriver.chrome.options import Options # Wrong: No download folder set, prompts not disabled options = Options() driver = webdriver.Chrome(options=options) driver.get('https://example.com') driver.find_element('id', 'download').click() # This may cause popup blocking or save to default folder # Right: Set preferences to avoid prompts and specify folder options = Options() options.add_experimental_option('prefs', { 'download.default_directory': '/path/to/folder', 'download.prompt_for_download': False }) driver = webdriver.Chrome(options=options)
Quick Reference
Tips for downloading files with Selenium:
- Use browser options to set
download.default_directory. - Disable download prompts with
download.prompt_for_download. - Use reliable locators like
id,css selector, orxpath. - Wait for downloads to complete before closing the browser.
- Check browser compatibility; Chrome and Firefox support these options.
Key Takeaways
Configure browser download preferences to automate file saving without popups.
Use Selenium to click the download link after setting download folder and options.
Always wait for the download to finish before closing the browser.
Use correct locators to find the download button or link reliably.
Test your setup with a sample file to ensure downloads work as expected.