How To Download File Using Selenium Python

Jolivé Hodehou

Posted On: August 12, 2022

view count507075 Views

Read time15 Min Read

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.

So, let’s get started!

Test Environment Setup

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.

What is the unittest framework?

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.

Installing the unittest framework

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.

  1. Type the following command in your terminal.
  2. The Python installation should look like this

    brew install Python

  3. Once you have installed it, make sure you have Python installed on your machine by typing in your terminal:
  4. or

    Python installed

  5. Next, you need to install Selenium on our machine. To do this, we will install pip using get-pip.py.
  6. This is a Python script that uses some bootstrapping logic to install pip.

    • Download the script from https://bootstrap.pypa.io/get-pip.py.
    • Open a terminal/command prompt, cd to the folder containing the get-pip.py file, and run:
    • Check if pip is installed by typing in your terminal
    • get-pip.py

  7. Once pip is installed, you can install Selenium in the same way.
  8. or depending on your permissions:

    For Python3:

  9. Add browser driver. WebDriver is an open source tool for automated testing of web applications on many browsers. It provides capabilities for navigating web pages, user input, executing JavaScript, and much more.
  10. We will run tests with Google Chrome, Mozilla Firefox, and Safari to download file using Selenium Python.

    Type the following command in your terminal.

    ChromeDriver will help us run our tests in Google Chrome. Without it, it is impossible to run Selenium test scripts in Google Chrome and automate any web application. This is why you need ChromeDriver to run test cases on the Google Chrome browser.

    Once the installation is complete, check if ChromeDriver is installed by typing the command:

    brew cask install chromedriver

  11. Now let’s install GeckoDriver, a web browser engine used in many applications developed by the Mozilla Foundation and the Mozilla Corporation. GeckoDriver is the link between your tests in Selenium and the Firefox browser.
  12. Check if GeckoDriver is installed by typing the command:

    geckodriver -v

    However, installation of the browser drivers is unnecessary if the tests will be executed on a cloud Selenium grid like LambdaTest.

LambdaTest is a cloud-based cross browser testing 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 LambdaTest YouTube Channel and stay updated with the latest tutorials around automated browser testing, Selenium testing, Cypress E2E testing, CI/CD, and more.

Downloading file using Selenium Python to a specific folder

In this section of the Python automation testing tutorial, we will consider the following test scenario to download file using Selenium Python:

  1. Go to the Selenium Playground.
  2. Click on the File Download button.
  3. In the Enter Data field, enter “How to download files using Selenium & Python?”
  4. Click on the Generate File button.
  5. Click on the Download button to download the file Lambdainfo.txt, which should contain “How to download files using Selenium & Python?”

This is what the structure of our project should look like.

structure of our project

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.

locators.py

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.

SeleniumPlaygroundPageLocators

data_field

generate_file

download_button

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.

Code Walkthrough:

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.

Here, we import all the classes in our locators.py file in the utils folder.

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:

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.

Code Walkthrough:

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.

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.

How to download file using Selenium Python in Chrome?

In our conftest.py file, we need to add some code. First, we will import the chromeOptions with the following import statement:

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:

  • options: This allows you to set the preferences of the Chrome browser.
  • download.default_directory: Allows to modify the default download directory. The default download folder will be the path defined in our PATH variable
  • add_experimental_option: Allows users to add these preferences to their Selenium webdriver object.

Our conftest.py file should now look like this:

Now you can run your test by typing in your terminal:

Your test should run as follows:

python3 -m unittest

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

Lambdainfo.txt

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

How to download file using Selenium Python in Firefox?

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.

More explanation:

  • Profile: The profile object is specific to FirefoxDriver and contains all the preferences to be defined.
  • browser.download.folderList: Tells not to use the default Downloads directory.
  • browser.download.manager.showWhenStarting: Turns of showing download progress.
  • browser.download.dir: Sets the directory for downloads.
  • browser.helperApps.neverAsk.saveToDisk: Informs Firefox to automatically download the files of the chosen mime types.

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:

Now, we can rerun our tests.

rerun our tests

How to download file using Selenium Python in Safari?

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:

  1. To enable the Develop menu in the Safari browser, click Safari > Preferences > Advanced tab. Select the Show Develop Menu check box. The Develop menu appears in the menu bar.
  2. turn on WebDriver support

  3. To enable Remote Automation, click Develop > Allow Remote Automation in the menu bar.
  4. Authorize safaridriver to launch the webdriver service that hosts the local web server. To permit this, run /usr/bin/safaridriver once manually and complete the authentication prompt.
  5. run usr bin safaridriver

  6. Now, we will need to make some changes before running the tests. Firstly we will change the default download folder for Safari. You can do this in the Safari preferences in the general tab.
  7. make some changes before running the tests

  8. Now, we will disable the download confirmation alerts. In the website tab, in the preferences.

download confirmation alerts

Now let’s add some code.

Our conftest.py file should now look like this:

Everything is ready to run the tests with Safari. After running your tests, the result should be as follows

ready to run the tests with Safari

How to download file using cloud Selenium Grid?

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.

LambdaTest 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 LambdaTest REST APIs (/user-files/download) to download files from LambdaTest cloud storage.

We will have to use the POST request to upload files to our LambdaTest storage and then the PUT request to download the user file from LambdaTest 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:

In our file test_download_file.py, we import JSON and request packages.

Then let’s add some code.

GitHub

Code Walkthrough:

In a new class TestAPIDownloadFile that we added to our file we declare two variables as follows:

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 LambdaTest profile page.

LambdaTest profile page.

Then we make a POST request to upload a file from our computer to the lambda storage.

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.

In a response variable, we save the response of our request that we display afterward by doing.

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.

Key identifies our file on the storage, so you have to replace it with the name of our file on the storage.

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.

test is run in your console

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

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 LambdaTest is your best bet.

Conclusion

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 LambdaTest.

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.

Frequently Asked Questions (FAQs)

How do I download Selenium for Python?

The Selenium Python bindings are easy to install. First, ensure the pip package manager is installed by typing pip into your terminal. Then run the following command:
pip install selenium

How do I save an image using Python Selenium?

Downloading images with Selenium WebDriver is possible. To start with, identify the image you want to download with the help of locators like id; class; xpath, and so on. Once it’s identified, use the open method for opening the file in write and binary mode.

Author Profile Author Profile Author Profile

Author’s Profile

Jolivé Hodehou

Jolivé Hodehou is a QA Engineer and Technical Writer. He enjoys contributing to the delivery of valuable products by building a strategic vision of quality and by being involved in the different aspects of the tester's role.

Blogs: 7



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free