0
0
Selenium Pythontesting~5 mins

File download handling in Selenium Python

Choose your learning style9 modes available
Introduction

We handle file downloads in tests to check if files are saved correctly. This helps ensure users get the right files from the website.

When testing if a report downloads correctly from a web app.
When verifying that an invoice PDF is generated and saved.
When checking if a CSV export feature works as expected.
When automating tests that require saving images or documents from a site.
Syntax
Selenium 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)

Set the download folder path to control where files save.

Disabling prompts lets downloads happen automatically.

Examples
This sets the download folder to '/tmp/downloads' and disables the download prompt.
Selenium Python
options.add_experimental_option('prefs', {
    'download.default_directory': '/tmp/downloads',
    'download.prompt_for_download': False
})
Clicks the button that starts the file download.
Selenium Python
driver.find_element('id', 'downloadButton').click()
Sample Program

This script sets a download folder, opens a page, clicks a download button, waits, and checks if the file is saved.

Selenium Python
import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# Set download folder path
download_folder = os.path.join(os.getcwd(), 'downloads')
os.makedirs(download_folder, exist_ok=True)

# Configure Chrome options for automatic download
options = Options()
options.add_experimental_option('prefs', {
    'download.default_directory': download_folder,
    'download.prompt_for_download': False,
    'download.directory_upgrade': True,
    'safebrowsing.enabled': True
})

driver = webdriver.Chrome(options=options)

try:
    driver.get('https://example.com/download')
    # Click the download link/button
    driver.find_element('id', 'downloadFile').click()

    # Wait for the file to download (simple wait for demo)
    time.sleep(5)

    # Check if file exists in download folder
    files = os.listdir(download_folder)
    if files:
        print(f"Download successful: {files[0]}")
    else:
        print("Download failed: No files found")
finally:
    driver.quit()
OutputSuccess
Important Notes

Always set a unique download folder per test to avoid confusion.

Use explicit waits or check file size to confirm download completion in real tests.

Adjust file name checks based on expected download file names.

Summary

Set browser preferences to control download folder and disable prompts.

Trigger downloads by clicking the right page element.

Verify downloads by checking the file system for saved files.