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).
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
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’.
The directory name to save the downloaded files.
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