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:

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.