【问题标题】:not able to download file in my project directory using selenium无法使用 selenium 在我的项目目录中下载文件
【发布时间】:2021-05-06 06:08:11
【问题描述】:

我无法在我的项目目录中下载文件我如何下载任何人帮助我解决这个问题 下面是我的代码

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

【问题讨论】:

  • 试试Thread.sleep(time in ms)
  • 文件已下载但在本地目录中我想在项目目录中下载如果我单击 downloadlink.click() 文件会在本地目录中自动下载我也提供项目目录的路径但文件未下载到文件夹中我给了那条路
  • 您在哪里以及如何“提供项目目录的路径”?
  • 解决方法是将该文件移动到您的适当位置
  • 我想在这个路径中移动那个文件-\\src\\test\\resources\\Download

标签: java selenium testng


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    相关资源
    最近更新 更多