selenium - 尝试验证文件是否已下载或未收到错误空指针异常时
问题描述
请参阅给定的图像 [ 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;
}
解决方案
推荐阅读
- android - 如何以表格形式显示excel数据?
- c# - AdomdConnection 可以接受令牌吗
- c++ - Qt 资源编译器:如何使用“-t”选项?
- amazon-s3 - 加速 AWS S3 视频上传:Cloudfront 还是传输加速?
- python - pywintypes.error: (5, 'SetFileSecurity', 'Access denied') 尝试通过 Anaconda 提示符运行 jupyter notebook 内核时
- javascript - 在 React 应用程序中使用访问令牌实现身份验证的最佳方法是什么?
- github - 如何将本地 GitHub Visual Studio 项目移动到 BitBucket
- c++ - 使用顶点着色器进行图像旋转
- sql - ORA-01802: 尽管 select 语句不包括日期操作,但 Julian 日期超出范围
- git - Git:我可以将更改从一个分支移动到另一个分支吗?