Webdriver实现下载功能,屏蔽掉windows弹出的对话框,FireFox下设置浏览器的属性,两种实现方式:

一、使用一个全新的FireFox浏览器打开Web应用,浏览器不带任何插件,也未对浏览器做任何默认配置,但需要对浏览器属性进行配置

// 获取浏览器的所有配置文件

ProfilesIni allProfiles = new ProfilesIni();
// "Selenium" 是之前创建的profile,不带任何插件,也未对浏览器做任何默认配置
FirefoxProfile profile = allProfiles.getProfile("selenium");
// 设置是否显示下载进度框
profile.setPreference("browser.download.manager.showWhenStarting", false);
// browser.download.folderList 设置Firefox的默认 下载 文件夹。0是桌面;1是“我的下载”;2是自定义
profile.setPreference("browser.download.folderList", 2);
// 设置浏览器的默认下载文件夹以及路径,如果使用自定义路径,必须要将browser.download.folderList设置为2
profile.setPreference("browser.download.dir", "E:\\");
// 设置哪种类型的文件下载不询问直接下载
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");

// 使用新浏览器selenium打开页面
WebDriver driver = new FirefoxDriver(profile);

二、使用安装了插件、做了默认配置的FireFox浏览器打开Web应用(即浏览器本身已设置下载时不弹出Windows对话框),完全不需要做任何配置,只需要使用默认的浏览器即可

ProfilesIni allProfiles = new ProfilesIni();
// "default" 是用户默认使用的浏览器,带有用户添加的各种插件,以及各种偏好设置
FirefoxProfile profile = allProfiles.getProfile("default");

//使用默认配置的浏览器default打开页面

WebDriver driver = new FirefoxDriver(profile);

 

posted on 2016-09-18 23:15  zw520ly  阅读(2431)  评论(1编辑  收藏  举报

导航