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

时间:2018-12-23 14:42:36

标签: watir

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"

1 个答案:

答案 0 :(得分:0)

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

另外,为了使其更整洁,我建议将路径位置放入一个单独的变量中。

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>