DEV Community

Cover image for How To Download File Using Selenium Python
JoliveHodehou for LambdaTest

Posted on • Originally published at lambdatest.com

How To Download File Using Selenium Python

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.

By automating the task of downloading a file, you can save time and effort and focus on the features that matter. However, there are various ways to do that.

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!

Try an online Selenium automation Grid to run your browser automation testing scripts. Our cloud infrastructure has 3000+ desktop & mobile environments. Try for free!

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.

    brew install Python
Enter fullscreen mode Exit fullscreen mode

The Python installation should look like this

2- Once you have installed it, make sure you have Python installed on your machine by typing in your terminal:

    Python --version
Enter fullscreen mode Exit fullscreen mode

or

    Python3 --version
Enter fullscreen mode Exit fullscreen mode

3- Next, you need to install Selenium on our machine. To do this, we will install pip using get-pip.py.

    python3 get-pip.py
Enter fullscreen mode Exit fullscreen mode
  • Check if pip is installed by typing in your terminal
     pip3 --version
Enter fullscreen mode Exit fullscreen mode

4- Once pip is installed, you can install Selenium in the same way.

   pip install selenium
Enter fullscreen mode Exit fullscreen mode

or depending on your permissions:

   sudo pip install selenium
Enter fullscreen mode Exit fullscreen mode

For Python3:

    sudo pip3 install selenium
Enter fullscreen mode Exit fullscreen mode

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

We will run tests with Google Chrome, Mozilla Firefox, and Safari to download file using Selenium Python.

Type the following command in your terminal.

    brew cask install chromedriver
Enter fullscreen mode Exit fullscreen mode

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:

    chromeDriver -v
Enter fullscreen mode Exit fullscreen mode

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

    brew cask install geckodriver
Enter fullscreen mode Exit fullscreen mode

Check if GeckoDriver is installed by typing the command:

    geckodriver -v
Enter fullscreen mode Exit fullscreen mode

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.

Test on Selenium testing tool Grid Cloud of 3000+ Desktop & Mobile Browsers.

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.

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"]')
Enter fullscreen mode Exit fullscreen mode

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"]')
Enter fullscreen mode Exit fullscreen mode


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


      generate_file = (By.ID, 'create')
Enter fullscreen mode Exit fullscreen mode


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

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)
Enter fullscreen mode Exit fullscreen mode

Try running your browser automation testing scripts on an online Selenium automation testing Grid. There are over 3000 desktop and mobile environments in our cloud infrastructure. Take a free test!

Code Walkthrough:

    from selenium.webdriver.common.keys import Keys
Enter fullscreen mode Exit fullscreen mode

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 *
Enter fullscreen mode Exit fullscreen mode

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

    import time
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Code Walkthrough:

    import unittest
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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?")
Enter fullscreen mode Exit fullscreen mode

Automate Cypress tests and perform browser automation cypress testing with LambdaTest. Our cloud infrastructure has 3000+ desktop & mobile environments. Try for free!

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:

    from selenium.webdriver.chrome.options import Options
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode
  • 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:

    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()
Enter fullscreen mode Exit fullscreen mode

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

    python3 -m unittest
Enter fullscreen mode Exit fullscreen mode

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?”.

Execute & analyse cypress cloud test scripts online. Deploy quality builds faster with 40+ browser versions on cloud.

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.

           #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)
Enter fullscreen mode Exit fullscreen mode

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.

        self.driver = webdriver.Firefox(firefox_profile=profile)
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Now, we can 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- To enable Remote Automation, click Develop > Allow Remote Automation in the menu bar.

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

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

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

Now let’s add some code.

           #Safari
           self.driver = webdriver.Safari()
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

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

This Cypress automation testing tutorial will help you learn the benefits of cypress automation, and how to install Cypress and execute Cypress automation testing over scores of browsers and operating systems online.

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:

    import requests
Enter fullscreen mode Exit fullscreen mode

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

    import requests
    import json
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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.


          #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)

Enter fullscreen mode Exit fullscreen mode

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'))
           ]
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

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"
           })

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)