如何使用selenium java识别chrome中正在进行的下载?

如何使用selenium java识别chrome中正在进行的下载?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,如何确定下载未完成/正在进行?因为我想在下载完成后关闭web驱动程序 WebDriver: Chrome 像这样的示例代码,假设chrome驱动程序设置就绪 @Test public void testDownloadPdf(){ // here is the selenium java code to download pdf. // when do i perform driver.close(); } 通常,最好在@AfterClass中关闭驱动程序。 如果您知道确切的文件名和预期位置

如何确定下载未完成/正在进行?因为我想在下载完成后关闭web驱动程序

WebDriver: Chrome
像这样的示例代码,假设chrome驱动程序设置就绪

@Test
public void testDownloadPdf(){
// here is the selenium java code to download pdf.
// when do i perform driver.close();
}

通常,最好在
@AfterClass
中关闭驱动程序。 如果您知道确切的文件名和预期位置,则可以使用
路径

String file_path_full = "C:\\users\\username\\downloads\\yourpdffile.pdf";
while(Files.notExists(Paths.get(file_path_full))) {Thread.sleep(10000);}

我发现这个。。。这对我来说很有效 这里我使用的是
FileUtils.sizeOfDirectory(downloadfolder)
它将返回目录的大小,并在每5秒检查一次大小是否相等。如果文件大小相等,则下载完成

private void waitForDownloadFinished() throws Exception {
            try {
                String path = "/Download/path/";
                if (path != null) {
                    File folder = new File(path);
                    long size, reSize;
                    do {
                        size = FileUtils.sizeOfDirectory(folder);
                        Thread.sleep(5000);
                        reSize = FileUtils.sizeOfDirectory(folder);
                    } while (size != reSize);
                    System.out.println("Download completed");
                }           
} catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                getWebBrowser().quit();
            }
        }

事实上,我不知道确切的文件名…我正在查找该pdf文件,一旦单击该文件,就会启动直接下载…模拟文件对象将是不知道该文件的解决方案name@Prashant你能从任何WebElement的属性中找到文件名吗?或者预测文件名?@pburgr是的,我可以预测文件名。然后您可以使用:String file\u path\u full=“包括文件名的预测文件位置”;