Selenium 当尝试验证文件是否已下载时,获取错误null指针异常
请查看给定的图像[分别成功下载每个文件,代码如下:Selenium 当尝试验证文件是否已下载时,获取错误null指针异常,selenium,Selenium,请查看给定的图像[分别成功下载每个文件,代码如下: buttonDownload.click(); //This is the code just to download 'Image' option. selectImageOption.click(); Thread.sleep(2300); selectImageOptionToDownload.click(); 要验证映像是否已成功下载?请尝试两种不同的方法,但在这两种情况下都会出现错误“Null Pointer Exception”
buttonDownload.click(); //This is the code just to download 'Image' option.
selectImageOption.click();
Thread.sleep(2300);
selectImageOptionToDownload.click();
要验证映像是否已成功下载?请尝试两种不同的方法,但在这两种情况下都会出现错误“Null Pointer Exception”
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 void isFileDownloaded(){
File downloadFolder=新文件(“C:\\Users\\dp\\Download”);//默认“下载”文件夹的路径。
List NAMESOFILES=Arrays.asList(downloadFolder.List());//此行出现错误“Null指针异常”
{
if(namesOfFiles.contains(“CM Supervisor Dashboard.png”))
{System.out.println(“下载”);}
其他的
System.out.println(“未下载”);
}
}
我尝试的另一个逻辑是:
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;
}
public boolean isFileDownloaded(字符串下载路径,字符串文件名){
布尔标志=假;
File dir=新文件(“C:\\Users\\dp\\Download”);
File[]dir_contents=dir.listFiles();
对于(int i=0;i
似乎list()和listFiles()正在返回null…检查路径?(“如果此抽象路径名不表示目录,或发生I/O错误,则返回null”):尝试/catch以查看是否引发I/O。使用while循环并尝试除外,检查下载是否在x秒内成功(>x秒,引发错误。)@pcalkins,你的链接帮助了我。非常感谢。@Yun,你的建议try catch block帮助了我解决了这个问题。非常感谢。下面的代码对我有效,将帮助其他人。public void isFileDownloaded(){try{File f=new File(“C:\\Users\\dp\\Downloads”);FileFilter filter=newfilefilter(){public boolean accept(File f){return f.getName().startsWith(“CM”);}};File[]files=f.listFiles(filter);System.out.println(“文件是:”);for(int i=0;i