Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for Download to finish in selenium webdriver JAVA

Tags:

Once clicking a download button, files will be downloaded. Before executing next code, it needs to wait until the download completes.

My code looks like this:

Thread.sleep(2000);
driver.findElement(By.xpath("//*[@id='perform']")).click();//click for download

Thread.sleep(20000);
//code to be executed after download completes
Readfile fileobj=new Readfile();
String checkfile=fileobj.checkfilename();

How can I make the webdriver wait until a download completes?

like image 816
qaepk Avatar asked Mar 28 '14 13:03

qaepk


People also ask

How wait till download is complete in Selenium?

There is no built-in to selenium way to wait for the download to be completed. The general idea here would be to wait until a file would appear in your "Downloads" directory.

How do I make Selenium wait 10 seconds?

We can make Selenium wait for 10 seconds. This can be done by using the Thread. sleep method. Here, the wait time (10 seconds) is passed as a parameter to the method.

How do you verify if the file is downloaded using Selenium Java?

Now, open Google Chrome (we are using Google Chrome as our default browser). Copy the URL: D:\data in the URL of your browser. Once you copy the URL, you will observe that the URL is updated as “file:///D:/data/ “. You will also observe the sample file created by you in the browser.

How do you know if a file is completely downloaded in Java?

You should create a checksum (an MD5 Sum, or SHA1 Sum) for the file on the server. Then after the download, run a the same checksum and the two values need to match. If you are downloading via Java, you can use the MessageDigest class to help you generate the digest.


2 Answers

A little late but this question has a good number of views, I thought it would be worth the time to answer it in case you haven't moved on or someone else comes across it.

I too ran into the same problem and thought I'd share. I was developing in python at the time but the same concept applies. You don't have to do the actual download using selenium. Rather than clicking on the element to start the download, you should consider retrieving the link and using built in functions to proceed from there.

The element you would normally click to begin the download should have a 'href' attribute that you should be able to read using selenium. This is the url pointing to the actual file. In python, it looks something like this:

    element = driver.find_element_by_id('dl_link')
    url = element.get_attribute('href')

From here you can use an http library to call the url. The important part here is that you set 'stream' to true so you can begin writing the bytes to a file. Make sure the file path contains the correct file extension and another thing, most operating systems don't allow you to name files with certain characters such as back slashes or quotations so heads up on that.

def download_file(url, file_path):
    from requests import get
    reply = get(url, stream=True)
    with open(file_path, 'wb') as file:
        for chunk in reply.iter_content(chunk_size=1024): 
            if chunk:
                file.write(chunk)

The program shouldn't continue until the download is complete making it no longer necessary to poll until it is complete.

I apologize for answering in a different language, in Java I believe you can use the HttpURLConnection API. Hope this helps!

like image 50
spatel4140 Avatar answered Sep 21 '22 09:09

spatel4140


do {

   filesize1 = f.length();  // check file size
   Thread.sleep(5000);      // wait for 5 seconds
   filesize2 = f.length();  // check file size again

} while (filesize1 != filesize2); 

where f is a File, filesize is long

like image 38
Erica King Avatar answered Sep 20 '22 09:09

Erica King