How do I handle file downloads and uploads with Selenium WebDriver?

Handling file downloads and uploads with Selenium WebDriver can be a bit tricky, but it's certainly possible. Here's how you can approach both scenarios.

Handling File Uploads

File uploads are the simpler of the two to handle with Selenium WebDriver. Assuming you have an <input type="file"> element on your webpage, you can simply send the file path to it. Here's how to do it in Python and JavaScript:


from selenium import webdriver
from import By

driver = webdriver.Chrome()

# Find the file input element
file_input = driver.find_element(By.ID, 'file-upload')

# Send the absolute file path to the file input element
file_path = '/path/to/your/local/file.txt'

# If there's an upload button, you can click it
upload_button = driver.find_element(By.ID, 'upload-button')

JavaScript (Node.js)

const { Builder, By } = require('selenium-webdriver');

(async function example() {
    let driver = await new Builder().forBrowser('chrome').build();

    try {
        await driver.get('');
        const fileInput = await driver.findElement('file-upload'));

        const filePath = '/path/to/your/local/file.txt';
        await fileInput.sendKeys(filePath);

        const uploadButton = await driver.findElement('upload-button'));
    } finally {
        await driver.quit();

In both examples, you need to replace /path/to/your/local/file.txt with the absolute path to the file you want to upload.

Handling File Downloads

File downloads can be more complex due to the need to interact with the file system and potentially the browser's download manager. Selenium itself does not have a built-in method to handle file downloads. However, you can configure the browser's settings to download files to a specific directory without prompting. Here's how you can do it in Python for Chrome:


from selenium import webdriver
from import Options
import os
import time

chrome_options = Options()
download_dir = '/path/to/download/directory'

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)
driver = webdriver.Chrome(options=chrome_options)

# Click on the download link or button
download_link = driver.find_element(By.ID, 'download-link')

# Wait for the download to finish (you may need a better way to wait)

# Verify the file has been downloaded
file_name = 'downloaded_file.txt'
assert os.path.isfile(os.path.join(download_dir, file_name))

# Close the browser

You need to replace /path/to/download/directory with the path to the directory where you want to save downloaded files and downloaded_file.txt with the expected filename.

Keep in mind that handling downloads in this way does not allow you to monitor the progress of the download or handle failures. For more complex scenarios, you might need to use an HTTP client library to perform the download instead of relying on the browser.

Additional Notes

  • When running tests that include file downloads, it's a good idea to use a unique download directory for each test to avoid conflicts and to clean up after tests.
  • For file uploads, the file must exist on the machine where the browser is running. This means that with remote WebDriver instances, you'll need to ensure the file exists on the remote machine or use a service that supports file uploads over the network.
  • File download settings can be configured similarly for other browsers, but the exact preference names and methods to set them might differ. Check the documentation for the specific WebDriver implementation you're using.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping