How to verify whether file downloading is completed before executing next code in java selenium?

Singh 25 Reputation points
2023-02-16T06:40:34.1866667+00:00

I am trying to download a file using java selenium. The download button is clicked and it takes time to initiate and complete the download from the website (time of download depends on size of file and speed of net).

However, the next code is executed immediately after the code driver.findElement(By.xpath("//*[@id='downloadReport']/div")).click(); without waiting for download to complete.

Thread.sleep(5000); is not useful as the time to download from site varies from 1 mintues to 15 minutes.

Also the download doesnot start immediately after execution of code

driver.findElement(By.xpath("//*[@id='downloadReport']/div")).click();

The file name is dynamic (different file name based on search data extracted from excel sheet SearchData.xls

Any help is highly appreciated.

The code is as under:-

package Basic;

import java.awt.AWTException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.Calendar;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class sftest2 {

public static void main(String[] args) throws IOException, InterruptedException, AWTException {
System.setProperty("webdriver.edge.driver", "D:\\Installer\\msedgedriver.exe");
File file =    new File("D:\\SearchData\\SearchData.xls");
FileInputStream inputStream = new FileInputStream(file);
HSSFWorkbook wb=new HSSFWorkbook(inputStream);
HSSFSheet sheet=wb.getSheet("SF_NSF");
int rowCount=sheet.getLastRowNum()-sheet.getFirstRowNum();
WebDriver driver = new EdgeDriver();
driver.manage().window().maximize();
driver.get("https://******************");
driver.navigate().to("https://******************************");
for(int i=1;i<=rowCount;i++) {
driver.findElement(By.name("cAccount")).sendKeys("Search");
driver.findElement(By.xpath("//*[@id='abcSearchAction']/div[1]/div[3]/div[4]/img")).click();
WebElement bName=driver.findElement(By.id("bName"));
borrowerName.sendKeys(sheet.getRow(i).getCell(0).getStringCellValue());
driver.findElement(By.xpath("//*[@id='search-button']/ul/li[1]/div/input")).click();
Thread.sleep(2000);
driver.navigate().refresh();
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(20));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[@id=\'load_projectTable\']")));
if(driver.getPageSource().contains("View 1 -"))
{
driver.findElement(By.xpath("//*[@id='downloadReport']/div")).click();
Thread.sleep(50000); // Doesnot want to wait for specified time, the next code should execute immediately after complete download of file called through above code
driver.findElement(By.xpath("//*[@id='footer-container']/div[3]/a")).click();
}

else
{
//driver.findElement(By.xpath("//*[@id='three-icons']/ul/li[3]/a/div")).click();
Calendar cal = Calendar.getInstance();
java.util.Date time = cal.getTime();
String timestamp = time.toString().replace(":", "").replace(" ", "");
System.out.println(time);
System.out.println(timestamp);
TakesScreenshot ts = (TakesScreenshot)driver;
File src=ts.getScreenshotAs(OutputType.FILE);
File trg=new File(".\\screenshots\\SF_1Cr_B_Search_"+timestamp+".png");
FileUtils.copyFile(src,trg);
//Thread.sleep(1000);
driver.findElement(By.xpath("//*[@id='footer-container']/div[3]/a")).click();
}
}
//Close the workbook
wb.close();
//Quit the driver
driver.quit();        
}
}
Microsoft 365 and Office | Development | Office JavaScript API
0 comments No comments
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.