How to download a file using Selenium and Python
Ganesh Hegde, Community Contributor - February 10, 2023
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.
Prerequisites:
- Users need to have a basic setup of Selenium and Python in their system.
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.
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:
- webdriver: Helps to perform browser-specific actions such as navigation, click, etc.
- time: Helps to pause the script at a desired time.
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:
- options: Helps set the preferences to Chrome browser.
- download.default_directory : Used for changing the default download directory. Example: The code specifies C:\Tutorial\down, which means that the file will be downloaded to that location.
- add_experimental_option: Allows users to add these preferences to their Selenium webdriver object.
Step 3: Create chrome driver object with options
driver = webdriver.Chrome(executable_path='./chromedriver',chrome_options=options);
Explanation of the code:
- driver: Creates Selenium chromedriver object with above mentioned options.
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'); gotit= driver.find_element_by_id('accept-cookie-notification'); gotit.click(); downloadcsv= driver.find_element_by_css_selector('.icon-csv'); downloadcsv.click(); time.sleep(5) driver.close() except: print("Invalid URL")
Explanation of the code:
- driver.get: Navigates to the URL where the relevant file is located
As soon as Selenium navigates to the website, they ask to accept the cookies, which must be done first to download the file.
- gotit.click() : clicks to accept cookies.
- downloadcsv: variable holds the locator for .csv file.
- downloadcsv.click(): On performing this action, Selenium downloads the file to the specific folder mentioned in Step 2.
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'); downloadcsv= driver.find_element_by_css_selector('.icon-csv'); gotit= driver.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.
Now you can navigate to the folder mentioned in Step 2, and get the Selenium downloaded file.
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:
- profile: The profile object is specific to FirefoxDriver which holds all the preferences to be set.
- browser.download.folderList: Setting this preference tells Selenium Webdriver to not use the default directory for downloading the file.
- browser.download.manager.showWhenStarting: Setting this preference turns off the showing of download progress.
- browser.download.dir: Setting this preference makes Selenium download the file to a specific folder (ex: C:\Tutorial\down).
- browser.helperApps.neverAsk.saveToDisk: Tells Firefox to automatically download the files of the selected mime-types. In this case, its application/octet-stream.
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 Firefox” later 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.
- firefox_profile: Sets the profile defined in the steps above.
- executable path: This value should point to the firefoxdriver binary file, if the binary is located in the root folder .\geckodriver.
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'); gotit= driver.find_element_by_id('accept-cookie-notification'); gotit.click(); downloadcsv= driver.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'); gotit= driver.find_element_by_id('accept-cookie-notification'); gotit.click(); downloadcsv= driver.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:
Navigate to the directory specified in Step 2 to get the Selenium downloaded file.
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 link
3. Open a new browser window. Then open the network tab:
4. Paste the URL copied and look for the network tab request:
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.