用webdriver启动Firefox浏览器时,我们可以通过FirefoxProfile来设置Firefox的一些属性。
如下面的代码设置了下载文件的放置路径等等。有个问题就出来了,想要设置的属性的key是什么?
通常Firefox的属性key-value都存放在 C:\Users\用户名\AppData\Roaming\Mozilla\Firefox\Profiles\XXX.default\prefs.js文件里。
可以先存下一份该文件,然后设置你想要的属性,prefs.js文件被更改。这个版本和之前的对比,就知道属性key-value了。
对比工具可以用WinMerge.
FirefoxProfile profile = new FirefoxProfile();
//设置下载文件放置路径,注意如果是windows环境一定要用\\,用/不行
profile.setPreference("browser.download.dir", "E:\\builds");
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.manager.showWhenStarting",
false);
// 禁止弹出保存框,value是文件格式,如zip文件
profile.setPreference(
"browser.helperApps.neverAsk.saveToDisk",
"application/zip,text/plain,application/vnd.ms-excel,text/csv,text/comma-separated-values,application/octet-stream,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document");