首页 > 解决方案 > 尝试验证文件是否已下载或未收到错误空指针异常时

问题描述

请参阅给定的图像 [ https://i.stack.imgur.com/VoiQE.png]使用以下代码成功单独下载每个文件:

buttonDownload.click();  //This is the code just to download 'Image' option.
selectImageOption.click();
Thread.sleep(2300);
selectImageOptionToDownload.click();

验证图像是否已成功下载?尝试了 2 种不同的方法,但在这两种情况下都会给出错误“空指针异常”。

public  void isFileDownloaded() {
File downloadFolder = new File("C:\\Users\\dp\\Download"); //Path of default 'Download' folder. 
    List<String> namesOfFiles = Arrays.asList(downloadFolder.list()); //At this line getting an error "Null Pointer Exception"
    {
    if(namesOfFiles.contains("CM Supervisor Dashboard.png"))
    {   System.out.println("Downloaded");}
    else
        System.out.println("not downloaded");
       }
    }

我尝试的另一个逻辑是:

public  boolean isFileDownloaded(String DownloadPath, String fileName) {
             boolean flag = false;
                File dir = new File("C:\\Users\\dp\\Download");
                File[] dir_contents = dir.listFiles();

                for (int i = 0; i < dir_contents.length; i++) {  //At this line getting an error 'Null Pointer Exception"
                    if (dir_contents[i].getName().equals("Dashboard"))
                        return flag=true;
                        }

                return flag;
            }

标签: selenium

解决方案


推荐阅读