Working with file download in Python Selenium WebDriver

The Blog To Learn Selenium and Test Automation

Working with file download in Python Selenium WebDriver

File download is nothing new and we often have to download files while executing automation tests. Python Selenium WebDriver is excellent in manipulating browser commands however lacks features to handle operating system native windows like automating file downloads.

While automating use cases we often encounter scenarios to download files like Ms Excel file, MS word document, text file, image, zip files, PDF files etc…

On click on the link or button of file download, a dialog box appears and asks users to select few options to save the file. This is not the browser HTML window but a system window controlled by the OS, which can not be handled or accessed by the Python Selenium since Selenium is a web browser automation tool.

The only thing that we can be able to do is set the default download files location for the browser and allow it to automatically download the files.
But we have a problem here; Python Selenium WebDriver will launch Firefox in default profile (Since no profile is specified explicitly). So in this case, we need to configure download location every time which is not expected in automation environment.

Instead of configuring it manually every time, we can instruct Selenium to set up Firefox Profile which ensures download files location is configured always. Have a look at below code.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference("browser.download.folderList",2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir","/data")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/vnd.ms-excel")
driver = webdriver.Firefox(firefox_options=self.options)

Here we are creating Firefox options object and set our preference. we can these preferences in ‘about:config’ in Firefox browser(Open new tab, type ‘about:config’ and click enter).

options.set_preference(“browser.download.folderList”, 2);

Values could be either 0, 1, or 2. Default value is ‘1’.

0 – To save all files downloaded via the browser on the user’s desktop.
1 – To save all files downloaded via the browser on the Downloads folder
2 – To save all files downloaded via the browser on the location specified for the most recent download

options.set_preference(“browser.download.manager.showWhenStarting”, false);

This setting allows the user to specify whether the Download Manager window should be displayed or not when file downloading starts. Default value is ‘true which is for ‘Display Download Manager window’.

options.set_preference(“browser.download.dir”,”/data”);

The directory name to save the downloaded files.

options.set_preference(“browser.helperApps.neverAsk.saveToDisk”,”application/octet-stream”);

A comma-separated list of MIME types to save to disk without asking what to use to open the file. Default value is an empty string.

MIME for few types of files are given below.

Text File (.txt) – text/plain
PDF File (.pdf) – application/pdf
CSV File (.csv) – text/csv or “application/csv”
MS Excel File (.xlsx) – application/vnd.openxmlformats-officedocument.spreadsheetml.sheet or application/vnd.ms-excel
MS word File (.docx) – application/vnd.openxmlformats-officedocument.wordprocessingml.document
Zip file (.zip) – application/zip

 

3 Responses

  1. Leonardo says:

    Hi. I’m using webdriver.Remote with Chrome desired capabilities.
    Is it possible to achieve the same behavior this way?

  2. archana says:

    Well explained in this Blog, Thanks for giving information.

    Please let me know, similarly for chrome and IE how to download a file.

    Regards,
    Archana

  3. admin says:

    Please visit our article http://allselenium.info/file-upload-using-python-selenium-webdriver/ to learn about file upload using Python Selenium Webdriver

Leave a Reply

Your email address will not be published. Required fields are marked *