bcar
bcar

Reputation: 815

ChromeOptions To Not Prompt For Download When Running RemoteDriver

I'm trying to debug why when running remote webdriver tests on a headless linux host download dialogs are presented in chrome. I believe the chrome version is 45.

Couple of Env Details

  1. Selenium 2.53 (gem)
  2. Selenium 2.53 Server Jar
  3. Chrome Driver 2.21

The framework/Tests are written in Ruby utilizing Capybara for driving web tests. Here is a brief snippet of how the remote driver is initialized.

            prefernces = {
          :download => {
            :prompt_for_download => false, 
            :default_directory => '/home/john.doe/Downloads/'
          }
        }
        caps = Selenium::WebDriver::Remote::Capabilities.chrome()
        caps['chromeOptions'] = {'prefs' => prefernces}

      http_client = Selenium::WebDriver::Remote::Http::Default.new
      http_client.timeout = 240
      options = {
        browser: :remote,
        url: "http://<server_url>:4444/wd/hub",
        desired_capabilities: caps,
        http_client: http_client
      }
      # Returns Remote Driver
      Capybara::Selenium::Driver.new(app, options)

I have verified via the hub that the chromeOptions are set, but when a file is downloaded, we're presented with a file dialog prompt.

I've burned the candle looking for a solution to this problem. Thanks for the help and consideration!

Upvotes: 0

Views: 2568

Answers (2)

Florent B.
Florent B.

Reputation: 42518

Here is an example to download a file with Capybara / Selenium / Chrome :

require 'capybara'
require 'selenium-webdriver'

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app,
    :url => "http://localhost:4444/wd/hub",
    :browser => :chrome,
    :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
      'chromeOptions' => {
        'prefs' => {
          'download.default_directory' => File.expand_path("C:\\Download"),
          'download.directory_upgrade' => true,
          'download.prompt_for_download' => false,
          'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
        }
      }
    )
  )
end

session = Capybara::Session.new(:chrome)
session.visit "https://www.mozilla.org/en-US/foundation/documents"
session.click_link "IRS Form 872-C"

sleep 20

Upvotes: 1

Thomas Walpole
Thomas Walpole

Reputation: 49880

try removing the / from the end of the default_directory and also setting directory_upgrade: true. Other than that make sure the browser has permission to write to the selected directory (note also the correct spelling of preferences)

 preferences = {
      :download => {
        :default_directory => '/home/john.doe/Downloads',
        :directory_upgrade => true,
        :prompt_for_download => false, 

      }
    }
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
  'chromeOptions' => {'prefs' => preferences}
)

Upvotes: 3

Related Questions