Selenium文件如何自动保存?-自动化测试
更新时间:2022-03-23 09:38:15 作者:多测师 浏览:245
使用selenium进行爬虫开发时,会遇到下载文件时弹窗显示保存还是打开的问题,这时候可以通过代码设置自动下载。
设置文件自动下载
通过网页开发工具分析文件的http响应的contentType头类型如:
Content-Type: vnd.ms-excel
这时候在代码中设置
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel");
设置文件下载路径
profile.setPreference("browser.download.folderList", 2);
// 路径分隔符g格式为双斜杠\\,如E:\\sipder\\file
//所以需要做响应的字符串处理
String downLoadPath = "E:\\sipder\\file"
profile.setPreference("browser.download.dir", downLoadPath );
profile.setPreference("browser.download.useDownloadDir", true);
profile.setPreference("browser.download.manager.showWhenStarting", false);
以上内容为大家介绍了自动化测试中的Selenium文件如何自动保存,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/