【问题标题】:How do validate the file name which is appended by 1 each time whenever I download the same file everytime using Selenium with Java?每当我每次使用 Selenium 和 Java 下载相同的文件时,如何验证每次附加 1 的文件名?
【发布时间】:2021-05-21 05:04:13
【问题描述】:

当我尝试下载同一个文件时,文件名每次都附加 1。例如: 第一次文件名是 b filedownload1805258.xls,第二次是 filedownload1805258(1).xls,第三次是 filedownload1805258(3).xls。 那么现在我该如何验证呢?

目前我的方法是:

public void onIVerifyIsDownloadedIn(String arg0, String arg1) throws Throwable {
    if (defaultDataJsonObject.containsKey(arg1)) {
        arg1 = defaultDataJsonObject.get(arg1).toString();
    }
    if (defaultDataJsonObject.containsKey(arg0)) {
        arg0 = defaultDataJsonObject.get(arg0).toString();
    }
    File filelocation=new File(arg1);
   File[] list= filelocation.listFiles();
    for(File file:list){
        try {
            if (file.getName().contains(arg0)) {
                Log.info("File is downloaded in given location");
                break;
            }
            else{
                Log.info("File is not downloaded.. Pleae check...!");
            }
        }
        catch (Exception e){

        }
    }



}

【问题讨论】:

  • 为了快速简单的解决方案,只需创建一个递归方法来检查文件是否存在并增加名称/编号。然后它应该调用自己递归检查递增的数字,这将一直持续到你得到一个不匹配的数字(你的有效文件名),并相应地为 ues 返回正确的文件路径。

标签: java selenium file fileinputstream


【解决方案1】:

处理这种情况最好的办法是先清空目录,然后调用download方法,这样每次都会有一个唯一的名字。

void delete(File f) throws IOException {
  if (f.isDirectory()) {
    for (File c : f.listFiles())
      delete(c);
  }
  if (!f.delete())
    throw new FileNotFoundException("Failed to delete file: " + f);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    相关资源
    最近更新 更多