How to download file using Selenium & Python

Publish date: 2024-05-19

Selenium is an open-source tool that automates web browsers. It provides a single interface that lets testers automate user actions using multiple programming languages like Ruby, Java, NodeJS, PHP, Perl, Python, and C#, among others. 

With Selenium, testers can automate a wide range of actions such as click, type, hover, upload files, download files – the list is quite exhaustive. 

One of the most common scenarios among internet users is downloading files off web pages. While Selenium doesn’t support this functionality natively, there is an easy way to use Selenium to download a file. 

So, for users Googling “selenium download file”, this article explores that exact scenario with a step-by-step tutorial.

Let’s consider the following scenario:

There is a .csv file located at the end of “Test on Right Mobile Devices” page, the intent is to download the file using Selenium and Python.

Download File Example

Prerequisites:

Now, let’s discuss how to download a file using Selenium and Python.

The challenge here is that the downloading process or approach is different in different browsers – such as Firefox and Chrome. So if a tester is using Selenium Webdriver to download files they need to have separate configurations for each browser.

 This guide will explain two approaches. With it, testers can use Selenium to download files to specific folders in both Chrome and Firebox.

Table of Contents

Download files to a specific folder in Chrome browser using Selenium

Step 1: Import required packages to Python test script

from selenium import webdriver import time

The code snippet above imports two packages:

Step 2: Set Chrome options

options = webdriver.ChromeOptions() ; prefs = {"download.default_directory" : "<directory_path>; #example: prefs = {"download.default_directory" : "C:\Tutorial\down"}; options.add_experimental_option("prefs",prefs);

Explanation of the code:

Step 3: Create chrome driver object with options

driver = webdriver.Chrome(executable_path='./chromedriver',chrome_options=options);

Explanation of the code:

Note: executable_path should be the relative path where the chromedriver is located. In this case, it is the root folder so it is mentioned as ./chromedriver

Step 4: Create a script to navigate to the website and click on download .csv

The steps above have set the preferences and imported all required packages. Next, the tester must write the script to navigate the website and click on the download file option.

from selenium import webdriver import time try:     driver.get('https://www.browserstack.com/test-on-the-right-mobile-devices');     gotitdriver.find_element_by_id('accept-cookie-notification');     gotit.click();         downloadcsvdriver.find_element_by_css_selector('.icon-csv');     downloadcsv.click();     time.sleep(5)         driver.close() except:      print("Invalid URL") 

Explanation of the code:

As soon as Selenium navigates to the website, they ask to accept the cookies, which must be done first to download the file.

Step 5: Run the test

When put together from step 1 to step 4, the code looks as below. On executing this script, the tester should be able to automate file download using Selenium and Python.

from selenium import webdriver import time options = webdriver.ChromeOptions() ; prefs = {"download.default_directory" : "C:\Tutorial\down"}; options.add_experimental_option("prefs",prefs); driver = webdriver.Chrome(executable_path='./chromedriver',chrome_options=options); try:     driver.get('https://www.browserstack.com/test-on-the-right-mobile-devices');     downloadcsvdriver.find_element_by_css_selector('.icon-csv');     gotitdriver.find_element_by_id('accept-cookie-notification');     gotit.click();         downloadcsv.click();     time.sleep(5)     driver.close() except:      print("Invalid URL")

After executing the script the file will be downloaded to the desired location.

File downloaded scenario

Now you can navigate to the folder mentioned in Step 2, and get the Selenium downloaded file.

Download file to specific folder in Chrome using Selenium

Try Selenium Testing for Free

Download files to a Specific folder in Firefox browser using Selenium

Step 1: Import the required packages

This step remains the same for both Chrome and Firefox.  Import required packages to the test scripts.

from selenium import webdriver import time

Step 2: Create Firefox Profile

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_to_downlaod_directory>") #Example:profile.set_preference("browser.download.dir", "C:\Tutorial\down") profile.set_preference("browser.helperApps.neverAsk.saveToDisk""application/octet-stream")

Explanation of the code:

Note: If testers are unsure about how to find the mime types that must be specified in preferences, scroll to the section on How to find the MIME type to specify when downloading files with Selenium WebDriver in Firefoxlater in this article.

Step 3: Create Firefox driver object with all preferences

driver = webdriver.Firefox(firefox_profile=profile,executable_path='.\geckodriver')

The code above passes two parameters namely: firefox_profile and executable path.

Step 4: Write a script to navigate to the webpage and download file

try:     driver.get('https://www.browserstack.com/test-on-the-right-mobile-devices');     gotitdriver.find_element_by_id('accept-cookie-notification');     gotit.click();     downloadcsvdriver.find_element_by_css_selector('.icon-csv');     downloadcsv.click();     time.sleep(5);     driver.quit(); except:     print ("Invalid URL")

This code snippet remains the same for both Chrome and Firefox.

Step 5: Execute the script

When put together from step 1 to step 3, the code looks as below. On executing this script, the tester should be able to automate file download using Selenium and Python.

from selenium import webdriver import time 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_to_downlaod_directory>") #Example:profile.set_preference("browser.download.dir", "C:\Tutorial\down") profile.set_preference("browser.helperApps.neverAsk.saveToDisk""application/octet-stream") driver = webdriver.Firefox(firefox_profile=profile,executable_path='.\geckodriver') try:     driver.get('https://www.browserstack.com/test-on-the-right-mobile-devices');     gotitdriver.find_element_by_id('accept-cookie-notification');     gotit.click();     downloadcsvdriver.find_element_by_css_selector('.icon-csv');     downloadcsv.click();     time.sleep(5)     driver.quit(); except:     print ("Invalid URL")

After execution of the script, Firefox downloads the file:

Download files using Selenium

Navigate to the directory specified in Step 2 to get the Selenium downloaded file.

Download files to specific folder in Selenium

How to find the MIME type to specify when downloading files with Selenium WebDriver in Firefox

In the Firefox preferences, one has to specify the MIME type. However, most times, the tester is not sure which MIME type to specify. Fortunately, there is a solution. 

Let’s consider the example depicted above. In order to find the MIME type, do the following:

  • Open Firefox browser. Navigate to URL https://www.browserstack.com/test-on-the-right-mobile-devices
  • Navigate to the .CSV download button and copy the download linkDownload file option in Firefox
  •           3. Open a new browser window. Then open the network tab:

    Open network tab in Firefox

                4. Paste the URL copied and look for the network tab request:

    Content type in Network Tab

    Here in the request, look for the first request. Therein, find the content-type. Mention it in Firefox preferences when writing the test script for downloading a file using Selenium. 

    Bear in mind Selenium WebDriver tests must be executed on real devices and browsers. Remember that device fragmentation is a major concern for every developer and tester. Every website has to work seamlessly on multiple device-browser-OS combinations. With 9000+ distinct devices being used to access the internet globally, all software has to be optimized for different configurations, viewports, and screen resolutions.

    In this state, no emulator or simulator can replicate real user conditions. Software needs to be tested on real devices so that they can work in real-world circumstances such as a low battery, incoming calls, weak network strength, and so on. If an in-house lab is not accessible, opt for a cloud-based testing option that offers real devices.

    BrowserStack’s cloud Selenium grid offers 2000+ real devices and browsers for automated testing. That means users can run tests on multiple real devices and browsers by simply signing up, logging in, and selecting the required combinations. Testers can also conduct Cypress testing on 30+ real browser versions across Windows and macOS. Detects bugs before users do by testing software in real user conditions with BrowserStack.

    ncG1vNJzZmivp6x7o77OsKqeqqOprqS3jZympmeXqralsY6dprCmnKSupXnFoqOeZaWotq%2BzjKycpZ2ensKuec%2Byq6Gnng%3D%3D