Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

On This Page
Unlock efficient testing with our comprehensive guide on downloading files using Selenium in Python.
Jolive Hodehou
January 21, 2026
Although browsers such as Firefox and Chrome have made downloading files easier, these downloads depend on users visiting a website and manually clicking a download button. This can be a problem if the user is interested in downloading multiple files.
Automating file downloads saves time and effort, allowing a focus on essential features. Multiple methods can accomplish this task.
The combination of Python and Selenium opens up many opportunities for automating various tasks like clicking, typing, hovering, and downloading files. Using Selenium with Python can automate this process of downloading files by identifying and clicking the download button.
In this Selenium Python tutorial, I will show you how to download files with Selenium WebDriver and Python using the unittest testing framework. You could then download any type of file and save it in a specific folder. If you’re looking to improve your Selenium interview skills, check out our curated list of Selenium interview questions and answers.
So, let’s get started!
You need to have the unittest framework, Selenium, and the different browser drivers on our machine. In this blog on how to download file using Selenium Python, we will consider running our tests on Chrome, Firefox, and Safari.
The unittest testing framework was initially inspired by JUnit and had a flavor similar to the major unit testing frameworks in other languages. This is the default Python test framework provided with the Python package and, therefore, the one most developers start their tests with. It can also be used for test automation, collection aggregation, etc.

Deliver immersive digital experiences with Next-Generation Mobile Apps and Cross Browser Testing Cloud
To have unittest installed on our machine, we first need to install Python.
But make sure you have Homebrew on your machine because we will use a macOS operating system in this tutorial on how to download file using Selenium Python.
brew install Python

Python --version
Python3 --version
pip install selenium
sudo pip install selenium
sudo pip3 install selenium
brew cask install chromedriver
chromeDriver -v
brew cask install geckodriver
geckodriver -v
python3 get-pip.py
pip3 --version
TestMu AI is a cloud-based platform that provides you with an online browser farm of 3000+ browsers and operating system combinations to perform cross browser compatibility testing at scale.
You can also Subscribe to the TestMu AI YouTube Channel and stay updated with the latest tutorials around automated browser testing, Selenium testing, Cypress E2E testing, CI/CD, and more.
In this section of the Python automation testing tutorial, we will consider the following test scenario to download file using Selenium Python:
This is what the structure of our project should look like.

In the blog on how to download file using Selenium Python, we will create three folders which are as follows: pages, tests, and utils.
Python packages are denoted by this __init__.py file (pronounced “dunder init”). It’s just an empty file inside a directory that says, “Hey, this is a Python package, and you can treat it as such, specifically for import statements.”
In our “utils” folder, we will create a locators.py file in which we will put our different locators.

from selenium.webdriver.common.by import By
class SeleniumPlaygroundPageLocators(object):
#locators
file_download = (By.XPATH, '//li[.="File Download"]')
data_field = (By.XPATH, '//*[@id="textbox"]')
generate_file = (By.ID, 'create')
download_button = (By.XPATH, '//*[@id="link-to-download"]')
Tests use locators to find elements on a page. Locators are simple query strings for finding elements. They will return all elements that match their query.
Selenium WebDriver supports many types of locators . Some of the most commonly used Selenium locators include— IDs, Names, Class Names, CSS Selectors, XPaths, Link Text, Partial Link Text, and Tag Name.
Now let’s look at how to get locators for the target items we need for our test scenario. We have created a SeleniumPlaygroundPageLocators class in which we have created variables for each element in which we will save our selectors for later use in our code.
file_download = (By.XPATH, '//li[.="File Download"]')

data_field = (By.XPATH, '//*[@id="textbox"]')

generate_file = (By.ID, 'create')

download_button = (By.XPATH, '//*[@id="link-to-download"]')

I have also created a directory called “pages“. In “pages,” we will create a file selenium_playground_page.py in which the page objects are written. To learn more about page objects, refer to our earlier blog on Page Object Model (POM) in Selenium Python.
from selenium.webdriver.common.keys import Keys
from utils.locators import *
import time
# Page objects are written in this module.
class Selenium_Playground_Page():
def __init__(self, driver):
self.locator = SeleniumPlaygroundPageLocators
self.driver = driver
def download(self, text):
self.driver.find_element(*self.locator.file_download).click()
self.driver.find_element(*self.locator.data_field).send_keys(text)
self.driver.find_element(*self.locator.generate_file).click()
self.driver.find_element(*self.locator.download_button).click()
time.sleep(5)
Code Walkthrough:
from selenium.webdriver.common.keys import Keys
The selenium.webdriver module provides all the WebDriver implementations. The Keys class provides methods through which you can access keys. You can refer to this blog on Selenium Keys to learn more about Keys class.
from utils.locators import *
Here, we import all the classes in our locators.py file in the utils folder.
import time
Python sleep() is a method of the Python time module. So, first, we must import the time module, and then we can use this method:
time.sleep(5)
In Python projects, it is common practice to create a “tests” directory under the project’s root directory to hold all the test scenarios. In tests, we have two files test_download_file.py and conftest.py.
import unittest
from selenium import webdriver
class Browser(unittest.TestCase):
def setUp(self):
PATH = "/Users/macbookair/Desktop/how_download_files_selenium_python/download" #This path must be modified according to your OS and the directory where you want to save your file
self.driver.get("https://www.lambdatest.com/selenium-playground/")
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main()
Code Walkthrough:
import unittest
The unittest module provides a rich set of tools for constructing and running tests. In this blog on how to download file using Selenium Python, we have used unittest, but several other Python testing frameworks can be used like PyTest, Robot, DocTest, etc.
PATH = "/Users/macbookair/Desktop/how_download_files_selenium_python/download"
We have created a PATH variable in which we save the path we want to save the file that we will download with Selenium Python.
Later we will add some code to our conftest.py file to be able to run the tests on our different browsers.
import unittest
from tests.conftest import Browser
from pages.selenium_playground_page import *
# I am using python unittest for asserting cases.
class TestDownloadFile(Browser):
def test_download(self):
page = Selenium_Playground_Page(self.driver)
download_file = page.download("How to download files using Selenium & Python?")
Note: Run your Python automated scripts on 3000+ browser environments. Try TestMu AI Now!
In our conftest.py file, we need to add some code. First, we will import the chromeOptions with the following import statement:
from selenium.webdriver.chrome.options import Options
Chrome Options class is used to control the properties of Chrome Driver and is used with combining Desired Capabilities. It helps you perform various operations, such as defining the folder where you want to save a download file.
Usage to create a Chrome driver instance:
#Google Chrome
options = Options()
prefs = {"download.default_directory" : PATH}
options.add_experimental_option("prefs",prefs)
self.driver = webdriver.Chrome(options=options)
Our conftest.py file should now look like this:
import unittest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class Browser(unittest.TestCase):
def setUp(self):
PATH = "/Users/macbookair/Desktop/how_download_files_selenium_python/download"
#Google Chrome
options = Options()
prefs = {"download.default_directory" : PATH}
options.add_experimental_option("prefs",prefs)
self.driver = webdriver.Chrome(options=options)
self.driver.get("https://www.lambdatest.com/selenium-playground/")
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main()
Now you can run your test by typing in your terminal:
python3 -m unittest
Your test should run as follows:

Once your test has run successfully, you should have the Lambdainfo.txt file in your download folder.

The file does contain the text “How to download files using Selenium & Python?”.

Deliver immersive digital experiences with Next-Generation Mobile Apps and Cross Browser Testing Cloud
Now, we will need to create a Firefox profile. Here is the code you will need to add to your conftest.py file if you plan to run your tests with Mozilla Firefox.
#Firefox
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", PATH)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
self.driver = webdriver.Firefox(firefox_profile=profile)
More explanation:
self.driver = webdriver.Firefox(firefox_profile=profile)
This line of code allows us to create a Firefox driver object with all the preferences.
Our conftest.py file should now look like this:
import unittest
from selenium import webdriver
class Browser(unittest.TestCase):
def setUp(self):
PATH = "/Users/macbookair/Desktop/how_download_files_selenium_python/download"
#Mozilla Firefox
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", PATH)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
self.driver = webdriver.Firefox(firefox_profile=profile)
self.driver.get("https://www.lambdatest.com/selenium-playground/")
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main()
Now, we can rerun our tests.

It is not necessary to download the Safari driver for Selenium WebDriver. Instead, the built-in Safari driver, safaridriver, is currently available in most Selenium client libraries. But before running web UI testing in Safari, ensure you enable remote automation in the Safari browser.
To allow remote automation in Safari, you must turn on WebDriver support:




Now let’s add some code.
#Safari
self.driver = webdriver.Safari()
Our conftest.py file should now look like this:
import unittest
from selenium import webdriver
class Browser(unittest.TestCase):
def setUp(self):
PATH = "/Users/macbookair/Desktop/how_download_files_selenium_python/download"
#Safari
self.driver = webdriver.Safari()
self.driver.get("https://www.lambdatest.com/selenium-playground/")
def tearDown(self):
self.driver.close()
if __name__ == '__main__':
unittest.main()
Everything is ready to run the tests with Safari. After running your tests, the result should be as follows

Every project is unique, and you must optimize your application for any configuration to provide a smooth and consistent user experience on all devices, browsers, and operating systems.
In an ideal world, developers would test their apps using real devices under real conditions. However, in-house labs tend to be small and thus cannot provide users with real-world experiences. Instead, they opt to use emulators or simulators designed to mimic the real feelings of users. Although these tools are great for app testing, they cannot replace real devices. In such cases, you must opt for a real device cloud testing solution that offers real devices.
TestMu AI cloud Selenium Grid offers an online device farm with 3000+ real devices and browsers to help you get the job done.
In this Python web automation tutorial on how to download file using Selenium Python, we will use TestMu AI REST APIs (/user-files/download) to download files from TestMu AI cloud storage.

We will have to use the POST request to upload files to our TestMu AI storage and then the PUT request to download the user file from TestMu AI storage.
Before we continue, we will install requests, a Python library that will allow us to make HTTP requests in Python. It abstracts the complexity of requests behind a nice simple API, so you can focus on interacting with services and consuming data in your application.
Run the following command from the terminal:
pip3 install requests
Once requests are installed, you can use them in your project. Importing requests looks like this:
import requests
In our file test_download_file.py, we import JSON and request packages.
import requests
import json
Then let’s add some code.
class TestAPIDownloadFile():
username = "username"
access_Key = "access key"
#POST_REQUEST
url = "https://api.lambdatest.com/automation/api/v1/user-files"
payload={}
files=[
('files',('Lambdainfo.txt',open('/Users/macbookair/Downloads/Lambdainfo.txt','rb'),'text/plain'))
]
headers = {
'authority': 'api.lambdatest.com',
'accept': 'application/json',
}
response = requests.request("POST", url, auth=(username, access_Key), headers=headers, data=payload, files=files)
print(response.text)
#PUT_REQUEST
url = "https://api.lambdatest.com/automation/api/v1/user-files/download"
payload = json.dumps({
"key": "Lambdainfo.txt"
})
headers = {
'accept': 'application/octet-stream',
'Content-Type': 'application/json'
}
response = requests.request("PUT", url, auth=(username, access_Key), headers=headers, data=payload)
open('/Users/macbookair/Documents/how_download_files_selenium_python/download/Lambdainfo.txt', 'wb').write(response.content)
print(response.text)
Code Walkthrough:
In a new class TestAPIDownloadFile that we added to our file we declare two variables as follows:
username = "username"
access_Key = "access key"
These variables will be used for identification in our API calls before accessing the endpoint.
You need to replace the value of the username and access key using the authentication data that you can retrieve from the TestMu AI profile page.

#POST_REQUEST
url = "https://api.lambdatest.com/automation/api/v1/user-files"
payload={}
files=[ ('files',('Lambdainfo.txt',open('/Users/macbookair/Downloads/Lambdainfo.txt','rb'),'text/plain'))
]
headers = {
'authority': 'api.lambdatest.com',
'accept': 'application/json',
}
response = requests.request("POST", url, auth=(username, access_Key), headers=headers, data=payload, files=files)
print(response.text)
Then we make a POST request to upload a file from our computer to the lambda storage.
files=[ ('files',('Lambdainfo.txt',open('/Users/macbookair/Downloads/Lambdainfo.txt','rb'),'text/plain'))
]
You must replace ‘Lambdainfo.txt‘ by the name of your file and ‘/Users/macbookair/Downloads/Lambdainfo.txt‘ by the path where the file is located.
response = requests.request("POST", url, auth=(username, access_Key), headers=headers, data=payload, files=files)
In a response variable, we save the response of our request that we display afterward by doing.
print(response.text)
#PUT_REQUEST
url = "https://api.lambdatest.com/automation/api/v1/user-files/download"
payload = json.dumps({
"key": "Lambdainfo.txt"
})
headers = {
'accept': 'application/octet-stream',
'Content-Type': 'application/json'
}
response = requests.request("PUT", url, auth=(username, access_Key), headers=headers, data=payload)
open('/Users/macbookair/Documents/how_download_files_selenium_python/download/Lambdainfo.txt', 'wb').write(response.content)
print(response.text)
Once our file has been uploaded to the lambda storage, we can now use our PUT requests to download it to the directory of our choice.
payload = json.dumps({
"key": "Lambdainfo.txt"
})
Key identifies our file on the storage, so you have to replace it with the name of our file on the storage.
open('/Users/macbookair/Documents/how_download_files_selenium_python/download/Lambdainfo_API.txt', 'wb').write(response.content)
Then we save the file in the directory of our choice. You must replace the specified path with the one where you want to save the file.
Once the test is run in your console, you can see the response to your requests.

And the file that has been downloaded in the defined directory.

If you’re a Python programmer looking to make a name for yourself in the automation testing domain, then the Selenium Python 101 certification program from TestMu AI is your best bet.

Deliver immersive digital experiences with Next-Generation Mobile Apps and Cross Browser Testing Cloud
This concludes the tutorial on how to download file using Selenium Python. When you automate your web tests using Selenium, you may need to test the complete functionality of a website or a web application. This means you will have to exercise features like downloading and uploading files, streaming video or audio, and reading/writing documents.
In this tutorial on Selenium Python testing, we saw how to download file using Selenium Python to a specific folder. Moreover, we learned to download file using Selenium Python in different browsers like Chrome, Firefox, and Safari. In the end, we looked at downloading file on a cloud Selenium Grid like TestMu AI.
I hope this has given you a good overview of how to download file using Selenium Python. If you have any questions or comments, please leave them in the section below.
Did you find this page helpful?
More Related Hubs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance