java - 无法使用 selenium 在我的项目目录中下载文件
问题描述
我无法在我的项目目录中下载文件我如何下载任何人帮助我的以下是我的代码
downloadlink.click();
String Resumedownloadpath="C:\\Users\\admin\\Downloads\\cb-comAutomation\\src\\test\\resources\\Download"
Assert.assertTrue(isFileDownloaded(Resumedownloadpath,"new_resume_001 (1).docx"),"Failed to download Expected document");//this is verify using assert
public boolean isFileDownloaded(String Resumedownloadpath, String fileName){//this function is validation
boolean flag = false;
File dir = new File(Resumedownloadpath);
File[] dir_contents = dir.listFiles();
for (int i = 0; i < dir_contents.length; i++) {
if (dir_contents[i].getName().equals(fileName))
return flag=true;
}
return flag;
}
解决方案
在启动浏览器之前,我们需要设置默认的下载路径,例如
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", System.getProperty("user.dir")+ File.separator + "externalFiles" + File.separator + "downloadFiles");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
ChromeDriver driver = new ChromeDriver(options);
以便将文件下载到指定文件夹。
推荐阅读
- javascript - 如何检查日期是否为假期
- algorithm - 给出排列数的递归公式?
- r - 当GBM模型保存和加载时,模型一次又一次加载时没有给出相同的预测值
- c++ - SDL 不在游戏窗口上渲染屏幕
- kubernetes - `kubectl logs counter` 没有按照官方 Kubernetes 示例显示任何输出
- python - 用给定字符串列表中的整数值替换每个字符串的程序
- java - Hibernate Criteria DetachedCriteria 和子查询
- python-3.x - 如何在熊猫 df 列中分配列表值?
- ios - 如何在 iOS Swift 中基于 CLLocation 在 arkit 中画一条线?
- ansible - blockinline 模块不尊重 insertafter