带有watir webdriver的Firefox 4:需要帮助使用helperApps.neverAsk来保存CSV而不提示

时间:2011-03-29 13:32:38

标签: firefox watir webdriver firefox4 watir-webdriver

我学习了如何将Firefox 4与watir和webdriver一起使用(在Win7 x64上),设置个人资料项目。例如:

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)

我尝试使用下面的示例,将CSV文件设置为始终下载到特定目录,从不打开。 上面的代码成功设置了自动下载到指定目录的所有文件,但设置browser.helperApps.neverAsk.saveToDisk无效:我仍然得到打开/保存问题。 脚本运行后,Firefox窗口仍然打开,我输入关于:config的URL。 我可以看到browser.helperApps.neverAsk.saveToDisk已正确设置为application.csv,但在firefox / options / options / applications中我没有看到CSV文件的条目。 看起来真正有效的菜单设置并没有真正与about:config设置绑定。 我做错了什么?

2 个答案:

答案 0 :(得分:11)

我已经为您完成了一些测试,遗憾的是CSV文件似乎没有标准的内容类型。您可以尝试传递逗号分隔的内容类型列表,希望其中一个适合您。对我来说,应用程序/八位字节流可以解决问题......

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"

答案 1 :(得分:4)

在Firefox 6+中,如果没有专门设置'browser.download.folderList'值,我就无法工作:

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

请参阅:http://watirwebdriver.com/browser-downloads/