Ruby Selenium Firefox 仅在 MacOS 上下载到 ~/Downloads
Ruby Selenium Firefox only downloading to ~/Downloads on MacOS
我无法让 Selenium 下载文件到 MacOS 上的指定文件夹。
运行:
- Ruby 3.0.1p64
- Rails6.1.3.1
- selenium-webdriver 4.0.0beta3
这是我当前的代码,它尝试在四个地方更改目录(非常感谢任何帮助):
require 'selenium-webdriver'
require 'fileutils'
Selenium::WebDriver.logger.level = :info
@download_dir = File.join(Dir.pwd, "lib/forecastdownloads")
FileUtils.mkdir_p @download_dir # Create the folder if missing
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = @download_dir
profile['browser.download.default_directory'] = @download_dir
options = Selenium::WebDriver::Firefox::Options.new(profile: profile)
options.add_argument('--headless')
options.add_preference('download.directory_upgrade', true)
options.add_preference('download.folderList', 2)
options.add_preference('download.prompt_for_download', false)
options.add_preference('download.dir', @download_dir)
options.add_preference('download.default_directory', @download_dir)
options.add_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, text/csv')
driver = Selenium::WebDriver.for :firefox, capabilities: options
driver.manage.timeouts.implicit_wait = 10 # seconds
wait = Selenium::WebDriver::Wait.new(:timeout => 15)
driver.get "https://...[the webpage]"
element = wait.until { driver.find_element(:class, "buttons-excel") }
element.click
driver.quit
使用上述代码,文件将下载到常规 ~/Downloads
文件夹而不是 @download_dir
。
问题出在 download.dir
的设置上。
而不是File.join(Dir.pwd, "lib/forecastdownloads")
,应该是:
profile['browser.download.dir'] = Dir.pwd + '/forecastdownloads'
我无法让 Selenium 下载文件到 MacOS 上的指定文件夹。
运行:
- Ruby 3.0.1p64
- Rails6.1.3.1
- selenium-webdriver 4.0.0beta3
这是我当前的代码,它尝试在四个地方更改目录(非常感谢任何帮助):
require 'selenium-webdriver'
require 'fileutils'
Selenium::WebDriver.logger.level = :info
@download_dir = File.join(Dir.pwd, "lib/forecastdownloads")
FileUtils.mkdir_p @download_dir # Create the folder if missing
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = @download_dir
profile['browser.download.default_directory'] = @download_dir
options = Selenium::WebDriver::Firefox::Options.new(profile: profile)
options.add_argument('--headless')
options.add_preference('download.directory_upgrade', true)
options.add_preference('download.folderList', 2)
options.add_preference('download.prompt_for_download', false)
options.add_preference('download.dir', @download_dir)
options.add_preference('download.default_directory', @download_dir)
options.add_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, text/csv')
driver = Selenium::WebDriver.for :firefox, capabilities: options
driver.manage.timeouts.implicit_wait = 10 # seconds
wait = Selenium::WebDriver::Wait.new(:timeout => 15)
driver.get "https://...[the webpage]"
element = wait.until { driver.find_element(:class, "buttons-excel") }
element.click
driver.quit
使用上述代码,文件将下载到常规 ~/Downloads
文件夹而不是 @download_dir
。
问题出在 download.dir
的设置上。
而不是File.join(Dir.pwd, "lib/forecastdownloads")
,应该是:
profile['browser.download.dir'] = Dir.pwd + '/forecastdownloads'