Firefox 4 with watir webdriver: Need help using helperApps.neverAsk to save CSV without prompting

邮差的信 提交于 2020-01-11 03:18:46

问题


I learned how to use Firefox 4 with watir and webdriver (on Win7 x64), setting profile items. Example:

profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.useDownloadDir"] = true
profile["browser.download.dir"] = 'D:\\FirefoxDownloads'
profile["browser.helperApps.neverAsk.saveToDisk"] = "application/csv"
driver = Selenium::WebDriver.for :firefox, :profile => profile
browser = Watir::Browser.new(driver)

What I try to do with the example below, is setting CSV files to be always downloaded to a specific directory, never opened. The code above succeeds in setting all the files automatically downloaded to the specified directory, but setting browser.helperApps.neverAsk.saveToDisk has no effect: I still get the open/save question. After the script runs, the Firefox window is still open, and I enter the URL about:config. I can see that browser.helperApps.neverAsk.saveToDisk was correctly set to application.csv , but in firefox/options/options/applications I don't see the entry for CSV files. It seems that the menu setting, that is really effective, is not really bound with the about:config setting. What am I doing wrong?


回答1:


I've done some testing of this for you, unfortunately there doesn't seem to be a standard content-type for CSV files. You can try passing a comma separated list of content-types, hopefully one of those work for you. For me it was application/octet-stream that did the trick...

require 'watir-webdriver'
require 'selenium-webdriver'

profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.useDownloadDir"] = true
profile["browser.download.dir"] = '/tmp'
profile["browser.helperApps.neverAsk.saveToDisk"] = "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream"
driver = Selenium::WebDriver.for :firefox, :profile => profile
browser = Watir::Browser.new(driver)

browser.goto "http://altentee.com/test/test.csv"



回答2:


In Firefox 6+, I couldn't get this to work without specifically setting the 'browser.download.folderList' value:

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2 #custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv, application/csv"
b = Watir::Browser.new :firefox, :profile => profile

See: http://watirwebdriver.com/browser-downloads/



来源:https://stackoverflow.com/questions/5473354/firefox-4-with-watir-webdriver-need-help-using-helperapps-neverask-to-save-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!