java - 如何使用 java-selenium webdriver 检查 zip 文件是否已成功下载?
问题描述
我正在从一个应用程序下载多个 zip 文件,每个文件都有不同的大小。显然,下载时间取决于文件大小。我有下面的java代码来根据文件大小检查下载。但它没有按预期工作。它只是等待10秒并通过继续下一组行来终止循环。有人可以帮我解决这个问题吗?
public void isFileDownloaded(String downloadPath, String folderName) {
String source = "downloadPath" + folderName + ".zip";
long fileSize1;
long fileSize2;
do {
System.out.println("Entered do-while loop to check if file is downloaded successfully");
String tempFile = source + "crdownload";
fileSize1 = tempFile.length(); // check file size
System.out.println("file size before wait time: " + fileSize1 + " Bytes");
Thread.sleep(10); // wait for 10 seconds
fileSize2 = tempFile.length(); // check file size again
System.out.println("file size after wait time: " + fileSize2 + " Bytes");
} while (fileSize2 != fileSize1);
}
等待时间前后的 fileSize 始终返回 43 个字节。
解决方案
这是我管理下载文件的方式:
public class Rahul {
String downloadDir = "C:\\Users\\pburgr\\Downloads\\";
public WebDriverWait waitSec(WebDriver driver, int sec) {
return new WebDriverWait(driver, sec);
}
public File waitToDownloadFile(WebDriver driver, int sec, String fileName) {
String filePath = downloadDir + fileName;
waitSec(driver, 30).until(new Function<WebDriver, Boolean>() {
public Boolean apply(WebDriver driver) {
if (Files.exists(Paths.get(filePath))) {
System.out.println("Downloading " + filePath + " finished.");
return true;
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Downloading " + filePath + " not finished yet.");
}
}
return false;
}
});
File downloadedFile = new File(filePath);
return downloadedFile;
}
}
推荐阅读
- c# - 如何使用 webclient 计算和显示下载速度?
- flutter - 如何为 qr_flutter 添加颜色渐变?
- docker - Pytest 不能在带有 Docker-Compose 的 Gitlab CI Docker 执行器中使用“docker-compose exec -T web_service pytest”运行
- database - 我无法更新数据库室中的项目列表并将该项目添加到收藏夹列表
- javascript - 窗口加载仅显示 1 件事
- bash - awk:第 1 行:在或附近出现语法错误,需要一些 bash 帮助
- c# - C# Automapper 如何使用嵌套映射创建列表?
- spring - 钥匙披风错误!登录用户没有电子邮件
- javascript - 如何为 ngrx 商店应用程序设置初始状态
- firebase - Flutter:获取当前登录 Firebase 用户以外的人的显示名称