如何在firefox中设置下载文件夹

How to set up a download folder in firefox

给出的结果http://watir.com/guides/firefox/

我设置下载文件夹如下图

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = File.expand_path("../../../A/Downloads", __FILE__)

但是它给了我

这样的警告信息
2018-12-23 20:04:23 WARN Selenium [DEPRECATION] :profile is deprecated. Use Selenium::WebDriver::Firefox::Options#profile= instead.

那么有人可以指导我为 firefox 设置下载文件夹的正确方法是什么吗?

实际上这也是我在我的程序中用于默认配置文件的另一行

options = Selenium::WebDriver::Firefox::Options.new
options.profile = "default"

对于 Windows,您需要规范化路径(检查下面的代码,gsub - 行 003:0)。 此外,为了没有下载弹出窗口 window - 您需要添加一些配置文件设置(请查看下方)。

另外,为了使其更清晰,我建议将路径位置取出到一个单独的变量中。

C:\Users\Sve>irb
irb(main):001:0> require "watir"
=> true
irb(main):002:0>
irb(main):003:0> custom_download_dir = File.expand_path("../../../A/Downloads", __FILE__).gsub('/', '\')
=> "C:\A\Downloads"
irb(main):004:0> Dir.entries(custom_download_dir) # => [".", ".."]
=> [".", ".."]
irb(main):005:0> profile = Selenium::WebDriver::Firefox::Profile.new
=> #<Selenium::WebDriver::Firefox::Profile:0x00000000039ff058 @model=nil, @native_events=true, @secure_ssl=false, @untrusted_issuer=true, @load_no_focus_lib=false, @additional_prefs={}, @extensions={}>
irb(main):006:0> profile['browser.download.dir'] = custom_download_dir
=> "C:\A\Downloads"
irb(main):007:0> profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf,image/jpeg"
=> "text/csv,application/pdf,image/jpeg"
irb(main):008:0> profile['browser.download.manager.showWhenStarting'] = "false"
=> "false"
irb(main):009:0> profile['browser.download.panel.shown'] = "false"
=> "false"
irb(main):010:0> profile['browser.download.folderList'] = 2
=> 2
irb(main):011:0>
irb(main):012:0> b = Watir::Browser.new :firefox, :profile => profile
=> #<Watir::Browser:0x50b784c8 url="about:blank" title="">
irb(main):013:0> b.goto "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_a_download2"
=> "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_a_download2"
irb(main):014:0> b.iframe(:id => "iframeResult").as.each {|x| puts x.href }
https://www.w3schools.com/images/myw3schoolsimage.jpg
=> [#<Watir::Anchor: located: true; {:id=>"iframeResult", :tag_name=>"iframe"} --> {:tag_name=>"a"}>]
irb(main):015:0> b.iframe(:id => "iframeResult").a.click
=> nil
irb(main):016:0> Dir.entries(custom_download_dir) # => [".", "..", "w3logo"]
=> [".", "..", "w3logo"]
irb(main):017:0>