Handling File Upload and Download with Selenium

Selenium is a powerful tool for automating web applications, but handling file upload and download tasks can be a bit tricky due to browser security restrictions and native OS dialogs. Fortunately, with the right approach, these tasks can be easily automated. Here's how to handle file upload and download in Selenium using practical methods.


Handling File Upload

When automating a file upload process, Selenium interacts with <input type="file"> elements directly. Here's a simple way to upload a file:

Example (Python + Selenium):

from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://example.com/upload")

# Locate the file input element and send the file path

upload_input = driver.find_element("id", "fileUpload")

upload_input.send_keys("C:/path/to/your/file.txt")

# Submit or proceed with the upload

driver.find_element("id", "submitBtn").click()

Important Tips:

This only works if the file input is not hidden or blocked by JavaScript.

Use send_keys() to simulate choosing a file.


Handling File Download

Downloading files is browser-dependent. For Chrome and Firefox, you can set preferences to auto-download files without prompting.


Chrome Example:

from selenium import webdriver

from selenium.webdriver.chrome.options import Options

options = Options()

prefs = {

    "download.default_directory": "C:/Users/YourName/Downloads",

    "download.prompt_for_download": False,

    "directory_upgrade": True

}

options.add_experimental_option("prefs", prefs)

driver = webdriver.Chrome(options=options)

driver.get("https://example.com/download")

# Trigger the download

driver.find_element("id", "downloadBtn").click()

Firefox Example:

from selenium import webdriver

from selenium.webdriver.firefox.options import Options

options = Options()

profile = webdriver.FirefoxProfile()

profile.set_preference("browser.download.folderList", 2)

profile.set_preference("browser.download.dir", "C:/Users/YourName/Downloads")

profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")  # Set MIME type

driver = webdriver.Firefox(firefox_profile=profile, options=options)

driver.get("https://example.com/download")

driver.find_element("id", "downloadBtn").click()


Limitations

Selenium cannot interact with OS-level file upload/download popups.

Use tools like AutoIt (Windows) or Robot Framework when dealing with native OS dialogs.


Conclusion

File upload and download automation in Selenium is achievable by directly sending file paths for uploads and customizing browser settings for downloads. By bypassing system dialogs and handling browser preferences smartly, testers can ensure smooth automation of these everyday tasks. 

Learn Selenium Java Training in Hyderabad

Read More:

Handling Dynamic Web Elements in Selenium

Building a Custom Selenium Framework with Java

Working with Action Class in Selenium

Using JavaScript Executor in Selenium

Parallel Test Execution Using TestNG

Visit our IHub Talent Training Institute

Get Direction

Comments

Popular posts from this blog

Tosca Installation and Environment Setup

Understanding Tosca TestCases and Modules

How Tosca Handles TestData Parameterization