File Upload

Introduction

Many web applications allow users to upload files such as documents, images, PDFs, spreadsheets, and other attachments. Automating file uploads is a common requirement in Selenium testing.

Unlike a real user, Selenium does not interact with the operating system’s file picker dialog. Instead, it uploads files by sending the absolute file path directly to the HTML file input element using the send_keys() method.

In this tutorial, you’ll learn how to automate file uploads using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.


What is File Upload?

A File Upload allows users to select a file from their computer and submit it to a web application.

Example:

Choose File

        │

        ▼

Select Local File

        │

        ▼

Upload to Website

In Selenium, this process is automated by sending the file path directly to the file input element.


Why Automate File Upload?

File upload automation helps you:

  • Test document uploads.

  • Verify image uploads.

  • Validate attachment functionality.

  • Test profile picture uploads.

  • Automate document management systems.


How Selenium Uploads Files

Selenium uses the send_keys() method on an HTML file input element.

Example:

file_input.send_keys("C:\\Files\\document.pdf")

The supplied path must be an absolute path.


Example

from pathlib import Path
from tempfile import TemporaryDirectory

from selenium import webdriver
from selenium.webdriver.common.by import By


# Topic: 35. File Handling - File Upload
# Practice site: https://the-internet.herokuapp.com/upload
# Run: pytest -s 35_examples/test_01_file_upload.py
#
# File upload fields use send_keys with the absolute path of the file to upload.
# Selenium does not interact with the operating system file picker.


def test_upload_file():
    driver = webdriver.Chrome()

    try:
        with TemporaryDirectory() as temp_dir:
            file_path = Path(temp_dir) / "selenium-upload-example.txt"
            file_path.write_text("Selenium file upload example", encoding="utf-8")

            driver.get("https://the-internet.herokuapp.com/upload")
            driver.find_element(By.ID, "file-upload").send_keys(str(file_path))
            driver.find_element(By.ID, "file-submit").click()

            uploaded_file = driver.find_element(By.ID, "uploaded-files").text
            assert uploaded_file == file_path.name
    finally:
        driver.quit()

Understanding the Code

Import Required Libraries

from pathlib import Path
from tempfile import TemporaryDirectory

from selenium import webdriver
from selenium.webdriver.common.by import By

These modules are used to:

  • Create a temporary file.

  • Generate the file path.

  • Launch the browser.

  • Locate web elements.


Create a Chrome Browser Instance

driver = webdriver.Chrome()

Starts a new Chrome browser session.


Create a Temporary Folder

with TemporaryDirectory() as temp_dir:

Creates a temporary directory that is automatically removed after the test finishes.

This avoids creating permanent files on your computer.


Create the Upload File

file_path = Path(temp_dir) / "selenium-upload-example.txt"

file_path.write_text(
    "Selenium file upload example",
    encoding="utf-8"
)

Creates a text file inside the temporary directory.

This file will be uploaded during the test.


Open the Upload Page

driver.get(
    "https://the-internet.herokuapp.com/upload"
)

Navigates to the file upload practice page.


Upload the File

driver.find_element(
    By.ID,
    "file-upload"
).send_keys(str(file_path))

Locates the file input element and sends the absolute file path.

Instead of opening the operating system’s file picker, Selenium directly assigns the file to the input element.


Submit the Upload

driver.find_element(
    By.ID,
    "file-submit"
).click()

Clicks the Upload button to submit the selected file.


Verify the Uploaded File

uploaded_file = driver.find_element(
    By.ID,
    "uploaded-files"
).text

assert uploaded_file == file_path.name

Reads the uploaded file name displayed on the page and verifies that it matches the uploaded file.

If the names are different, the test fails.


Close the Browser

driver.quit()

Closes the browser and ends the WebDriver session.


Practical Example

Suppose an HR portal allows employees to upload their resumes.

The automation script:

  • Creates or selects a resume file.

  • Uploads it.

  • Submits the form.

  • Verifies that the uploaded file name is displayed.


Automation Testing Example

Consider an online banking application where customers upload identity documents.

The automation script:

  • Uploads a document.

  • Clicks Submit.

  • Verifies that the upload was successful.


Real-World Example

File uploads are commonly used in:

  • Job portals

  • Banking applications

  • Insurance websites

  • Government portals

  • Document management systems

  • Social media websites

  • Enterprise web applications

Examples include uploading resumes, profile pictures, invoices, contracts, PDFs, spreadsheets, and supporting documents.


Advantages of File Upload Automation

  • Simulates real user uploads.

  • Verifies upload functionality.

  • Supports document validation testing.

  • Eliminates manual testing.

  • Improves automation coverage.


Common Mistakes Beginners Make

Trying to Automate the Operating System File Picker

Selenium cannot interact with native operating system file dialogs.

Instead, send the absolute file path directly to the file input element using send_keys().


Using a Relative File Path

Most browsers expect an absolute path.

Using a relative path may cause the upload to fail.


Uploading a Non-Existent File

Always ensure that the file exists before calling send_keys().


Forgetting to Verify the Upload

Always verify that the application successfully received the uploaded file.


Best Practices

  • Use send_keys() to upload files.

  • Always provide an absolute file path.

  • Create temporary files for automated tests whenever possible.

  • Verify the uploaded file after submission.

  • Keep upload tests independent and repeatable.


Conclusion

File upload is one of the most frequently automated features in Selenium. Instead of interacting with the operating system’s file picker, Selenium uploads files by sending the absolute file path directly to the HTML file input element. Mastering file uploads is essential for testing modern web applications that handle documents, images, and other user attachments.


Frequently Asked Questions (FAQs)

Can Selenium automate the Windows file picker?

No. Selenium cannot interact with native operating system file picker dialogs.


How does Selenium upload a file?

Selenium uploads a file by sending its absolute file path to the HTML file input element using send_keys().


Why is an absolute file path required?

Browsers expect the complete file location to correctly attach the file for upload.


Can Selenium upload dynamically created files?

Yes. As shown in this example, Selenium can upload files that are created during the test execution.


Where is file upload commonly used?

File uploads are commonly used in job portals, banking applications, insurance systems, document management platforms, social media websites, government portals, and enterprise applications.


Key Takeaways

  • Selenium uploads files using the send_keys() method.

  • Selenium does not automate the operating system file picker.

  • Always provide an absolute file path.

  • Temporary files are useful for creating repeatable automated tests.

  • Always verify that the uploaded file is displayed correctly after submission.

  • File upload automation is an essential Selenium skill for testing modern web applications.