sscirrus
sscirrus

Reputation: 56779

Wrong download directory for Selenium Firefox Geckodriver

I've been trying to get Selenium to download a file to a specific folder, but to no avail.

Here is my current driver setup with lots of simultaneous attempts to influence the download directory:

#<Selenium::WebDriver::Firefox::Options:0x00007f94374c0bd0 @debugger_address=nil, 
@options={
  :browser_name=>"firefox",
  :args=>[],
  :prefs=>{
    "download.folderList"=>2,
    "download.dir"=>"./downloads",
    "download.directory_upgrade"=>true,
    "download.prompt_for_download"=>false,
    "download.default_directory"=>"./downloads",
    "plugins.plugins_disabled"=>"Chrome PDF Viewer", 
    "browser.helperApps.neverAsk.saveToDisk"=>"application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, text/csv"
  }
},
@profile=#<Selenium::WebDriver::Firefox::Profile:0x00007f94374c0e28 @model=nil, 
@additional_prefs={
  "browser.download.folderList"=>2, 
  "browser.download.manager.showWhenStarting"=>false, 
  "browser.download.downloadDir"=>"./downloads", 
  "browser.download.dir"=>"./downloads",
  "browser.download.directory_upgrade"=>true, 
  "browser.download.prompt_for_download"=>false, 
  "browser.download.default_directory"=>"./downloads"
}, @extensions={}>>

I have tried:

I went through the selenium-webdriver docs and Mozilla Webdriver docs, but I cannot find references to set the download directory.

Upvotes: 0

Views: 1534

Answers (2)

sscirrus
sscirrus

Reputation: 56779

The custom directory path has to be absolute, formed as shown:

  • On Mac, ~Downloads folder would be /Users/[me]/Downloads.

  • In Rails, say you want to put the downloads in lib/custom_downloads, the directory specified should be Dir.pwd + '/lib/custom_downloads'.

The code that worked for me:

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2
profile['browser.download.dir'] = Dir.pwd + '/forecastsdir'
profile['browser.helperApps.neverAsk.saveToDisk'] = ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel')
options = Selenium::WebDriver::Firefox::Options.new(profile: profile, args: ['-headless'])
driver = Selenium::WebDriver.for :firefox, options: options

Many thanks to @Rajagopalan (+1) for assistance.

Upvotes: 1

Rajagopalan
Rajagopalan

Reputation: 6064

Try this and let me know whether it works.

profile = Selenium::WebDriver::Firefox::Profile.new(ENV['APPDATA'] + '\Mozilla\Firefox\Profiles\3deyh6ub.default-release')
profile['browser.download.dir'] = custom_download_dir
profile['browser.download.folderList'] = 2
options = Selenium::WebDriver::Firefox::Options.new(profile: profile)
driver = Selenium::WebDriver.for :firefox, options: options

and check out this path Mozilla\Firefox\Profiles\3deyh6ub.default-release and see what's the folder name and give the folder name accordingly and it will work.

Upvotes: 1

Related Questions