Intermediate Chapter 11 · 9 min read

File Upload & Download

Upload files via file input elements, handle download dialogs, configure download directory, and verify downloaded files.

File Upload & Download

File uploads use the sendKeys() method on file input elements — no need for OS-level dialogs. Downloads require configuring browser preferences to auto-save files and skip the download dialog.

FileUploadDownloadTest.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.testng.Assert;
import org.testng.annotations.*;
import java.io.File;
import java.time.Duration;
import java.util.*;

public class FileUploadDownloadTest {

    WebDriver driver;

    @Test
    public void testFileUpload() {
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        driver.get("https://www.selenium.dev/selenium/web/upload.html");

        // Find file input and send the file path
        WebElement fileInput = driver.findElement(By.id("upload"));
        String filePath = new File("src/test/resources/test-file.txt")
            .getAbsolutePath();
        fileInput.sendKeys(filePath);

        // Submit
        driver.findElement(By.id("go")).click();
        System.out.println("File uploaded successfully");

        driver.quit();
    }

    @Test
    public void testFileDownload() {
        // Configure Chrome to auto-download
        String downloadDir = System.getProperty("user.dir") + "/downloads";
        new File(downloadDir).mkdirs();

        ChromeOptions options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<>();
        prefs.put("download.default_directory", downloadDir);
        prefs.put("download.prompt_for_download", false);
        prefs.put("download.directory_upgrade", true);
        options.setExperimentalOption("prefs", prefs);

        driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

        // Navigate to a page with downloadable content
        driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");
        driver.findElement(By.id("file-1")).click();

        // Wait for download to complete
        try { Thread.sleep(3000); } catch (Exception e) {}

        File[] files = new File(downloadDir).listFiles();
        Assert.assertNotNull(files);
        Assert.assertTrue(files.length > 0, "File should be downloaded");
        System.out.println("Downloaded: " + files[0].getName());

        driver.quit();
    }
}
test_file_upload_download.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import os, time, pytest

@pytest.fixture
def driver():
    d = webdriver.Chrome()
    d.implicitly_wait(10)
    yield d
    d.quit()


def test_file_upload(driver):
    driver.get("https://www.selenium.dev/selenium/web/upload.html")

    file_input = driver.find_element(By.ID, "upload")
    file_path = os.path.abspath("tests/test-file.txt")

    # Create test file if not exists
    if not os.path.exists(file_path):
        os.makedirs(os.path.dirname(file_path), exist_ok=True)
        with open(file_path, 'w') as f:
            f.write("Test content for upload")

    file_input.send_keys(file_path)
    driver.find_element(By.ID, "go").click()
    print("File uploaded successfully")


def test_file_download():
    """Configure Chrome for auto-download"""
    download_dir = os.path.join(os.getcwd(), "downloads")
    os.makedirs(download_dir, exist_ok=True)

    options = Options()
    prefs = {
        "download.default_directory": download_dir,
        "download.prompt_for_download": False,
        "download.directory_upgrade": True,
    }
    options.add_experimental_option("prefs", prefs)

    driver = webdriver.Chrome(options=options)
    driver.implicitly_wait(10)

    try:
        driver.get("https://www.selenium.dev/selenium/web/downloads/download.html")
        driver.find_element(By.ID, "file-1").click()

        time.sleep(3)  # Wait for download

        files = os.listdir(download_dir)
        assert len(files) > 0, "File should be downloaded"
        print(f"Downloaded: {files[0]}")
    finally:
        driver.quit()

Selenium Intermediate File Upload & Download

Written by PV

© 2026 All Rights Reserved