【问题标题】:Wrong download directory for Selenium Firefox GeckodriverSelenium Firefox Geckodriver 的下载目录错误
【发布时间】:2021-07-26 14:52:29
【问题描述】:

我一直试图让 Selenium 将文件下载到特定文件夹,但无济于事。

这是我当前的驱动程序设置,有很多同时尝试影响下载目录:

#<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={}>>

我试过了:

  • 下载目录(我用的是Mac):
    • /lib/downloads
    • 下载
    • ./下载
  • 以上尝试设置下载目录的多种组合。

我浏览了selenium-webdriver docsMozilla Webdriver docs,但找不到设置下载目录的参考。

【问题讨论】:

    标签: ruby selenium selenium-webdriver selenium-firefoxdriver


    【解决方案1】:

    试试这个,让我知道它是否有效。

    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
    

    并查看此路径Mozilla\Firefox\Profiles\3deyh6ub.default-release 并查看文件夹名称并相应地提供文件夹名称,它将起作用。

    【讨论】:

    • 这对我不起作用。首先我在 Mac 上,所以我的个人资料文件夹是 /Users/[me]/Library/Application\ Support/Firefox/Profiles/l7kfmhse.default-1524776181722。我把它放进去,上面写着No such file or directory ...那个文件夹。
    • 如果我完全删除该文件夹(只需执行 Firefox::Profile.new),它会显示 Unable to create a driver with parameters: {:profile=&gt;#&lt;Selenium::WebDriver::Firefox::Profile:0x00007fd861d5db80 @model=nil, @additional_prefs={"browser.download.dir"=&gt;"forecastsdir", "browser.download.folderList"=&gt;2}, @extensions={}&gt;} (ArgumentError)
    • @sscirrus 我已经编辑了代码。现在可以试试吗?”
    • 新代码运行但不下载任何文件。我必须添加 profile['browser.helperApps.neverAsk.saveToDisk'] = ('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel') 以便它接受下载。但是,该文件将转到 ~/Downloads。
    • 我刚刚想通了。发布答案。非常感谢您的帮助,@Rajagopalan! +1
    【解决方案2】:

    自定义目录路径必须是绝对的,格式如下:

    • 在 Mac 上,~Downloads 文件夹为 /Users/[me]/Downloads

    • 在Rails中,假设你想把下载放在lib/custom_downloads,指定的目录应该是Dir.pwd + '/lib/custom_downloads'

    对我有用的代码:

    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
    

    非常感谢@Rajagopalan (+1) 的帮助。

    【讨论】:

    • 不客气
    猜你喜欢
    • 2017-08-30
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2021-06-24
    • 2018-03-31
    相关资源
    最近更新 更多