Determining file MIME types to autosave using Firefox & Watir-WebDriver

Update: Jari Bakken points out you can do this on Mac/Linux with one terminal command:

curl -I http://dl.dropbox.com/u/18859962/temp.csv | grep Content-Type

Introduction

When using Firefox with Watir WebDriver, there’s a nifty trick to set certain file types to automatically download so it’s not necessary to deal with troublesome ‘save as’ dialog boxes.

The problem with it is that you need to explicitly tell Firefox which file types you want to automatically save. Whilst this seems like an easy idea in concept, in practice it’s a bit harder as the web site serving the file can affect the MIME type which Firefox uses to determine what to do.

I recently found a way to determine what MIME type Firefox uses for a particular file so that this can be automatically captured. Here’s how:

1) Navigate to the site in Firefox and click on the link to the file (here’s an example CSV file). This should display a dialog such as the one below, where you’ll want to click ‘Save File’ and ‘Do this automatically for files like this from now on’

2) What this does is create an entry in the Applications tab of the settings to always download the file. Unfortunately, this doesn’t show us the MIME type which is what we need to specify this in our Firefox WebDriver profile.

3) What we want to do is open a file in our Firefox profile called mimeTypes.rdf. To find your Firefox profile on your disk, first open the page ‘about:support’ in Firefox, and click the ‘Show in Finder’ or ‘Open Containing Folder’ button.

4) Then navigate into your profile directory and open mimeTypes.rdf in your favorite text editor. We simply need to find the MIME type we just added, which is my case is found by searching for ‘excel’. The following text is included:

 <RDF:Description RDF:about="urn:mimetype:text/csv"
                   NC:fileExtensions="csv"
                   NC:description="Microsoft Excel.app Document"
                   NC:value="text/csv"
                   NC:editable="true">
    <NC:handlerProp RDF:resource="urn:mimetype:handler:text/csv"/>
  </RDF:Description>

The thing we’re looking for is NC:value=”text/csv“. This is our MIME type.

5) Now we simply add this MIME type to our file types we want to automatically save. This is done when creating the Firefox Watir-WebDriver instance:

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2 #custom location
profile['browser.download.dir'] = "#{Dir.pwd}/downloads"
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv"

Note: you can add multiple MIME types to this value in the same string separated by commas.

Summary

While it’s a fairly long winded process, this takes the guess work out of ensuring you automatically download files of certain types.

10 thoughts on “Determining file MIME types to autosave using Firefox & Watir-WebDriver

  1. Thanks for this post! Here are a couple extra pieces of info that could be useful to WW noobs like me:
    1.on windows, the slashes in the path MUST be backslashes (ugh)
    2.to use the profile once you create it:
    myBrowser=Watir::Browser.new(:firefox, :profile => myProfile)
    3.I wanted a blanket of all pdfs, zips, and text files, and I found what I think should cover all my bases here:

    http://www.webmaster-toolkit.com/mime-types.shtml

    4.Mozilla profile tweaks which may be helpful in the future:

    http://kb.mozillazine.org/About:config_entries

    • Thanks for letting me know.
      This exact issue was driving me nuts yesterday (path contained ‘/rads’) and therefore would revert to documents and settings.
      At least I know why now.

  2. I totally appreciate this blog. This is the best documentation currently available, as far as I can tell, on how to deal with file downloads in watir-webdriver for firefox.

  3. You’ve just saved me from hours of trying other things… turns out my “doc” file is an “application/unknown”. Thanks.

  4. Worked like a charm :). I spent hours on working this out when I started using watir back in time, and things have simplified so much.
    [For anyone else’s quick reference, few more mime types are: excel – application/excel (some csv files also use this mime), pdf – application/pdf, xml – text/xml]
    Cheers !!!

  5. Hi Alister! I like your blog because of it’s work around over nature over various problems.
    It seems, It will solve my problem too, but can any buddy please tell me
    where and how

    profile = Selenium::WebDriver::Firefox::Profile.new
    profile[‘browser.download.folderList’] = 2 #custom location
    profile[‘browser.download.dir’] = “#{Dir.pwd}/downloads”
    profile[‘browser.helperApps.neverAsk.saveToDisk’] = “text/csv”

    code should be written ?

    I’m windows7 user + selenium webdriver/ RC+ JAVA, then how and where
    I have create FF profile manually and I’m accessing it in my script like

    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile profile = profile.getProfile(“SELENIUM”);
    driver = new FirefoxDriver(profile );

    So how shall I alter my code?

    One more thing I have observed is, each time when I’m running script, I check profile folder-> it creates new folder like
    C:\Users\pooja\AppData\Local\Temp\anonymous1779712952984282267webdriver-profile
    And next time
    C:\Users\pooja\AppData\Local\Temp\anonymous6043159092177087903webdriver-profileofile
    Will it be affecting?
    Any help will be appreciated. Thanks in advance.

    • I’m sorry because I couldn’t find how to edit my previous query, I’m posting the edited query again, please refer this one.

      I want to know is, where and how

      profile = Selenium::WebDriver::Firefox::Profile.new
      profile[‘browser.download.folderList’] = 2 #custom location
      profile[‘browser.download.dir’] = “#{Dir.pwd}/downloads”
      profile[‘browser.helperApps.neverAsk.saveToDisk’] = “text/csv”

      code should be written ?

      I’m windows7 user + selenium webdriver/ RC+ JAVA.
      I have created FF profile manually and I’m accessing it in my script like

      ProfilesIni profile = new ProfilesIni();
      FirefoxProfile profile = profile.getProfile(“SELENIUM”);
      driver = new FirefoxDriver(profile );

      So how shall I alter my code?

      One more thing I have observed is, each time when I’m running script, I check profile folder-> it creates new folder like
      C:\Users\pooja\AppData\Local\Temp\anonymous1779712952984282267webdriver-profile
      And next time
      C:\Users\pooja\AppData\Local\Temp\anonymous6043159092177087903webdriver-profileofile
      Will it be affecting?
      Any help will be appreciated. Thanks in advance.

Comments are closed.