Search code examples
google-chromeselenium-webdriverpluginscapability

Selenium WebDriver (Ruby): How to download PDF files?


When running WebDriver (Ruby) using Chrome, what are the specific command-line switches or desired_capabilities to disable Chrome's built-in PDF viewer.

In other words, how does one programmatically configure Chrome to download PDF files rather than open using its internal PDF-viewer plugin?


Solution

  • Updated answer: Chrome 61 : Ruby 2.3 : ChromeDriver 2.32.498537

          download_prefs = {
            prompt_for_download: false,
            default_directory: 'desired/download/path'
          }
    
          plugin_prefs = {
            always_open_pdf_externally: true
          }
    
          options = Selenium::WebDriver::Chrome::Options.new
          options.add_preference(:download, download_prefs)
          options.add_preference(:plugins, plugin_prefs) 
          driver = Selenium::WebDriver.for :chrome, options: options