1

In case of a download initiated by Javascript (usually by some Javascript code that submits a form, which may be dynamically added to the page), none of the standard method for forcing a file download in Watir worked for me: I still get the browser file-download confirmation pop-up, which cannot be scripted in Watir. Worse, it looks like even conventional methods that worked when following a convential link to download a file, are now broken in newest browsers, please see this other question: How to download a file using Watir 6.0

Any suggestion on how to do it?

2 Answers2

1

The documentation for that is here now: http://watir.com/guides/downloads/

prefs = {
  download: {
    prompt_for_download: false,
    default_directory: '/path/to/dir'
  }
}

b = Watir::Browser.new :chrome, options: {prefs: prefs}

Best practice, though, is not to use Watir or Selenium to handle downloads. Ideally the creation of and access to the file is handled in a unit or integration test. Watir interacts with browsers, whereas downloads are partially an operating system function. This is to say that it may not be possible to do exactly what you need.

Sam
  • 1,205
  • 1
  • 21
  • 39
titusfortner
  • 4,099
  • 2
  • 15
  • 29
  • Yep, I've tried this but it does not work or does not work anymore with latest firefox and chrome. Tried both and I still get the browser file-download pop-up. I am not doing TDD, just web scraping and so I need to actually get the file. – Antonio Bonifati 'Farmboy' Jan 30 '18 at 17:48
  • I'm saying you might be better off bypassing the browser & OS dialog box by doing something like this: https://stackoverflow.com/a/35609783/4072371 I'm not sure what to say about this not working as several others have reported success with this setup. I'm planning to put a lot of extra time into updating the configuration code in the next version of Watir, so I'll personally dig into this more soon. – titusfortner Feb 08 '18 at 17:45
0

If was having a similar problem and I enable the logger.level it helped me to determine if the prefs were even being set for the "chromeOptions"

Selenium::WebDriver.logger.level = :info

prefs = {
        download: {
            prompt_for_download: false,
            default_directory: "#{FigNewton.download_files}"
        }
    }

args = ['--ignore-certificate-errors', '--disable-popup-blocking', '--disable-translate', '--disable-infobars']

browser = Watir::Browser.new :chrome, options: {prefs: prefs, args: args }

I'm not saying this will resolve your issue, however just wanted to provide you the information about the logger.level. I find it helpful.

niartseoj
  • 112
  • 7