Scenario :Suppose in a portal we have a link to download file every time user click on link and new file get downloaded how to check which is the latest file?

  • selenium is used to interact with a browser, you probably want another tool to interact with a filesystem to check if the file is there. You should be able to do this fairly easily through code or a small script. If you want to interact with the desktop, then it gets a little harder, but I'm not sure why you would want to do that if you just want to check if a file is there with a size greater than 0.
    – mrfreester
    Aug 28, 2017 at 18:26
  • @mrfreester i would like to do that as i mention any time user click on download link a new file get downloaded so if user click on link for 3 time 3 file are getting downloaded eg:File_randomenumber_34567,File_randomenumber_34557,File_randomenumber_34807 so i need to search for the latest file and read the data from the file and compare data that of portal.
    – Mr.Learner
    Aug 28, 2017 at 18:35
  • I think you might be better off searching stack overflow for questions on how to read data from files as this is more of a general programming type of a question.
    – mrfreester
    Aug 28, 2017 at 20:19
  • 1
    You should ask a more specific question. The information you have provided is not enough. There are thousands of possible ways each relevant to its particular case.
    – Alexey R.
    Aug 29, 2017 at 10:41

2 Answers 2


You don't use Selenium.

Remember Selenium is the library that handles the communication between your test framework and the browser. Once the browser clicks to download a file Selenium has done its part and now you need to use your test framework to locate the file and perform any further non-browser actions.

  • 1
    Thank you it worked as mention in your comment @Chris Kenst
    – Mr.Learner
    Apr 21, 2019 at 5:34
  • @Mr.Learner Great! Can you accept this answer as the final one? May 2, 2019 at 22:24
  • 1
    yes i accept as the final answer that framework should handle it @Christ Kenst
    – Mr.Learner
    Jun 2, 2019 at 6:05
  • @Mr.Learner great, you just need to click that check mark next to the answer to mark it as such. Jun 3, 2019 at 6:34
  • 1
    Thanks @Chris Kenst
    – Mr.Learner
    Jun 27, 2019 at 9:20

To check latest downloaded file you first need to download the file within a custom or specific directory. You can take help from here, where the procedure is explained using Selenium 4.9.1 Java and for Chrome & Firefox Browser v113. Coming to the verification part - you have to iterate over all the downloaded file and check the lastModifiedTime of the file as below:

  Path file = Paths.get("/home/downloads/file123.txt");

  BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);

  System.out.println("lastModifiedTime: " + attr.lastModifiedTime());

Now create a current timestamp variable and calculate the difference. The file which has the lowest difference - is the latest file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.