The Issue
Selenium does not support the capability to interact with the browser confirmation pop-up window that is displayed when you download a file. Furthermore, different browsers have different default behavior for File Downloads. For instance, Firefox has a system confirmation dialog box pop up but Microsoft’s Edge browser doesn’t have a confirmation pop-up by default. You can also tweak the individual browser settings to turn these confirmation pop-ups on/off.
The Answer
A few extra lines of basic code in your Selenium script can bypass the confirmation dialog box and we can download the file to a specific location and even run a quick validation to verify if the desired file was downloaded.
The Code
The code below navigates the browser to a site (https://file-examples.com/index.php/sample-documents-download/sample-doc-download/) where you can download a sample file. After completing the download, we will confirm if the desired file was downloaded:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
require 'selenium-webdriver' # Creating a new browser profile profile = Selenium::WebDriver::Firefox::Profile.new # Setting the path to custom directory (1 = default path, 2 = custom, 0 = desktop) profile['browser.download.folderList'] = 2 # You can set this value whether to display the Downloads window or not when a download begins profile["browser.download.manager.showWhenStarting"] = false # Setting MIME(Multipurpose Internet Mail Extension) type of files to download automatically profile["browser.helperApps.neverAsk.saveToDisk"] = "application/msword" # Setting the download directory path @download_dir = File.join(Dir.pwd, "downloads").gsub(%r{/}) { "\\" } unless File.directory?(@download_dir) FileUtils.mkdir_p @download_dir end profile['browser.download.dir'] = @download_dir # Setting Firefox Geckodriver path Selenium::WebDriver::Firefox.driver_path = "FIREFOX-GECKODRIVER-PATH" # Configuring Firefox profile browser = Selenium::WebDriver.for :firefox, profile: profile # Opening the website from where the file is downloaded browser.get "https://file-examples.com/index.php/sample-documents-download/sample-doc-download/" sleep 2 # Locating the download button element download_button = browser.find_element :xpath, '//*[@id="table-files"]/tbody/tr[1]/td[5]/a[1]' sleep 2 download_button.click sleep 5 #Validating if the file is downloaded and not empty puts (Dir.empty? ("C:\\Users\\prane\\test_download")) ? "File is not downloaded.":"File is downloaded successfully!" puts (File.size(Dir["C:/Users/prane/test_download/*"].first) > 0) ? "Downloaded file is not empty.":"Downloaded file is empty." browser.close |
It is highly recommended to set the MIME type “profile[‘browser.helperApps.neverAsk.saveToDisk’]” to avoid pop-ups from displaying. For example if you are downloading a pdf file the MIME type for “profile[‘browser.helperApps.neverAsk.saveToDisk’] = ‘application/pdf'”. If the MIME-type is not configured the right way, the system dialog box still shows up and the test fails.
The Result
When the above code is run as a ruby (.rb) file, the following steps occur sequentially:
- Opens the web page
- Navigates to the file downloading website
- Clicks on the target file and downloads the file to the specified location
- Checks if the file is downloaded and not empty
The Takeaway
Downloading a file through different browsers, though a hurdle in Selenium can be resolved by adding simplistic lines of code that enable the user to download a file to the desired location from any browser.