首页 > 解决方案 > 无法使用 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;
    }

标签: javaseleniumtestng

解决方案


在启动浏览器之前,我们需要设置默认的下载路径,例如

 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);

以便将文件下载到指定文件夹。


推荐阅读