如何在Java Selenium中等待文件下载

如何在Java Selenium中等待文件下载,java,selenium,Java,Selenium,在处理给定文件之前,如何检查该文件是否已下载 我已成功创建了一个Chrome Webdriver实例,其中包含一个不同的默认下载文件夹,代码如下: Path currentWorkingDir = Paths.get("").toAbsolutePath(); Map<String, Object> prefs = new HashMap<String, Object>(); prefs.put("download.default_direc

在处理给定文件之前,如何检查该文件是否已下载

我已成功创建了一个Chrome Webdriver实例,其中包含一个不同的默认下载文件夹,代码如下:

Path currentWorkingDir = Paths.get("").toAbsolutePath();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory",
          System.getProperty("user.dir") + File.separator + "DownloadedTickets");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
System.setProperty("webdriver.chrome.driver", currentWorkingDir + "\\chromedriver.exe");
WebDriver ticketWebDriver = new ChromeDriver(options);
Path currentWorkingDir=Path.get(“”).toAbsolutionPath();
Map prefs=新的HashMap();
prefs.put(“download.default_目录”,
System.getProperty(“user.dir”)+File.separator+“downloaddedtickets”);
ChromeOptions选项=新的ChromeOptions();
选项。设置实验选项(“prefs”,prefs);
System.setProperty(“webdriver.chrome.driver”,currentWorkingDir+“\\chromedriver.exe”);
WebDriver ticketWebDriver=新的ChromeDriver(选项);
但是当我尝试访问文件而不等待下载时,我会得到NullPointerException


在访问该文件之前,我如何检查该文件是否存在于文件夹中?

共享我在使用Selenium时发现的一个问题的解决方案,并对另一个问题的版本稍加修改:

public static void waitUntilFileIsDownloaded(String rootDownloadFolder, String fileName) {
        File dir = new File(rootDownloadFolder);
        File[] dirContents = dir.listFiles();
        //System.out.println("Dir Contents" + dirContents);

        boolean fileDownloaded = false;
        boolean hasRanOnce = false;

        do {
            dirContents = dir.listFiles();
            if (!hasRanOnce) {
                System.out.print("Downloading file.\r");
                hasRanOnce = true;
            }
            for (int i = 0; i < dirContents.length; i++) {
                if (dirContents[i].getName().equals(fileName)) {
                    // File has been found, it can now be deleted:
                    fileDownloaded = true;

                }
            }
        } while (!fileDownloaded);
        System.out.print("File download complete.\n");
    }

创建自定义预期条件:

public ExpectedCondition<Boolean> filepresent() {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            File f = new File("D:\\program.txt"); 
            return f.exists();
        }
        @Override
        public String toString() {
          return String.format("file to be present within the time specified");
        }
    };
}

这与下载有什么关系?也许至少可以说这是在做什么?您可以使用自定义解释等待并使用f.exists();你可以试着每x秒打开一次,直到你不再得到NPE,而不是这么多的代码。
public ExpectedCondition<Boolean> filepresent() {
    return new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            File f = new File("D:\\program.txt"); 
            return f.exists();
        }
        @Override
        public String toString() {
          return String.format("file to be present within the time specified");
        }
    };
}
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(2));
    wait.until(pageobject.filepresent());