Selenium Webdriver - browser preferences for downloading files

In this article you will find and introduction to browser’s profiles/preferences and quick solution for managing downloading files from script level.

|

Introduction

Some time ago I developed Selenium tests for feature, where one of the important parts was exporting reports to file from web browser, then comparing theirs content with reference files. There were a lot of export files (about 300), grouped in some categories, and some files from different categories had the same default names. It was necessary to export files to different folders during executing automation tests, because exported files couldn’t be overwritten. The major problems were:

  • how to change browser download path from the script?
  • how to disable download pop-up dialog during saving file?
  • and how to do that for different web browsers?

What to do for example with OS-level dialog in Selenium? First way is to fix the result and – by the way –making your code and tests more complex. Second method is to find the reason and create a workaround.

I prefer second way, hope you too, so let’s proceed with the instruction.

In this article I’m going to introduce you in browser’s profiles/preferences and show quick solution for managing downloading files from script level.

If you are reading this article, I bet that you know well what Selenium is, so we can go directly to the essence of problem

Mozilla Firefox

Whenever you tried to download file via Firefox, for sure you have seen this popup dialog:

As long as that’s not big deal for “real” user, it might be a problem for automation tests in Selenium. Maybe you know that even Firebug doesn’t work against window since it’s not HTML or JavaScript based. Selenium works similar – only for browser automation. So anywhere non-browser window will be displayed – like above OS-level download dialog – Selenium is not able to “see” that, therefore we cannot do anything with this window from Selenium level.

Now you could think:  we can select Save File radio button, click checkbox Do this automatically for files like this from now on, and voilà, problem solved.  That would be enough for regular using if you don’t want to see this window for this kind of files. But this is not good enough for automation in Selenium:

  • this is another configuration issue, so every time we will try to run a test on another device we need to remember about that, however we like convention over configuration, don’t we?
  • most important thing: actually that workaround is not working with Selenium

Why? Maybe you observed that Selenium always run “pure” instance of browsers, without any add-ons or user settings. You don’t see your bookmarks, favorites or installed plugins.

This is the moment when I should say something about Firefox Profile. What is that? This is a place where Firefox saves personal information such as: bookmarks, settings etc. I’ll show you short Python script which create and configure profile with disabled download pop-up dialog box and set download path as we want to:

    def __get_firefox_driver(self, download_dir):
        fp = webdriver.FirefoxProfile()
        fp.set_preference("browser.download.manager.showWhenStarting", False)
        fp.set_preference("browser.download.manager.showAlertOnComplete", False)
        fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
        fp.set_preference("browser.download.dir", download_dir)
        return webdriver.Firefox(firefox_profile=fp)

In the first line of this method object of FirefoxProfile is created. Then you can see using set_preference method for following entries:

  • ("browser.download.manager.showWhenStarting", False) – disabling Download Manager window when a download begins
  • ("browser.download.manager.showAlertOnComplete", False) – popup window at bottom right corner of the screen will not appear once all downloads are finished
  • ("browser.helperApps.neverAsk.saveToDisk", "text/csv") – list of MIME types to save to disk without asking what to use to open the file – in this case .csv files. This setting is actually disabling download dialog box
  • ("browser.download.dir", download_dir) –  determines the directory to download, where download_dir is a path.

The last line is creating webdriver object with the profile created before.

Note: A profile has to be created before webdriver object. There is no possibility to change profile parameters when webdriver object already exists.

Link to all Firefox config entries: link

Google Chrome

Downloading files from Selenium point of view is simpler in Google Chrome than in Mozilla Firefox. The main reason is fact that the download dialog box is disabled by default in Chrome, so we don’t have to write any extra code to avoid this window. But what about download path issue? Can we manipulate it from script? Of course we can. Implementing simple code uses ChromeOptions class to managing options specific to Chrome driver.

    def __get_chrome_driver(self, download_dir):
        chrome_options = webdriver.ChromeOptions()
        prefs = {"download.default_directory": download_dir}
        chrome_options.add_experimental_option("prefs", prefs)

        return webdriver.Chrome(executable_path=Config.PATH_TO_CHROME_DRIVER, chrome_options=chrome_options)

 After creating ChromeOptions object, download path is set in JSON-style parameter. And this one is enough for the issue described in this article. Same as in Firefox case, Chrome Options object has to be created before webdriver.

More info about Chrome Options: link

IE and other browsers

Unfortunately, since in IE 8 there is no option to disable download dialog. For this reason and the fact that IE webdriver doesn’t support manipulation browser preferences, there is no simple way to manage downloading files in Selenium.

I don’t have any experience with automation other browsers like Edge or Safari, as far as I am concerned, for those browser’s options or profiles are still unavailable.

Summary

Browser’s options and profiles give us a great tool to control browser appearance and behavior. Small part of this of this functionality has been shown in this article, but it has much more capabilities than just downloading files.

Paweł Motyka

pawel.motyka@j-labs.pl

Did you like this article?

Selenium Webdriver - browser preferences for downloading files