Skip to content

Instantly share code, notes, and snippets.

@daluu
Last active November 19, 2020 10:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daluu/4411221 to your computer and use it in GitHub Desktop.
Save daluu/4411221 to your computer and use it in GitHub Desktop.
A simple example of downloading file with Java outside of Selenium to workaround Selenium limitation in file downloads.
/* an example of file download in Java w/ minimal amount of code, library imports,
* and object instantiations especially if you wrap/encapsulate code like example below.
* NOTE: sample code has been tested to work but not thoroughly tested. Use at your own risk.
*/
import java.net.*;
import java.io.*;
//be sure to delete file after working with it. filenamePrefix ~ "test_", file extension ~ ".jpg", include the "."
public String downloadFile(String url, String filenamePrefix, String fileExtension) throws Exception{
//request setup...
URLConnection request = null;
request = new URL(url).openConnection();
//extract session cookie from Selenium and use with HTTP request calls in Java
//example below assumes server is running PHP hence we get PHP session ID
//assumes you have direct access to WebDriver here
request.setRequestProperty("Cookie", "PHPSESSID="+driver.manage().getCookieNamed("PHPSESSID").getValue());
//add other headers as needed...
//make the request to download file to disk as temp file and return path to file
//can add a check for HTTP status code 200 if needed, but sample here skips, just check response output (file)
InputStream in = request.getInputStream();
File downloadedFile = File.createTempFile(filenamePrefix, fileExtension);
FileOutputStream out = new FileOutputStream(downloadedFile);
byte[] buffer = new byte[1024];
int len = in.read(buffer);
while (len != -1) {
out.write(buffer, 0, len);
len = in.read(buffer);
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
in.close();
out.close();
return downloadedFile.getAbsolutePath();
}
@daluu
Copy link
Author

daluu commented Jan 16, 2013

This is simple working code. Can be fine tuned to do HTTP status code checking, do try/catch/finally error handling, and check that downloaded file is greater than 0 kb.

@daluu
Copy link
Author

daluu commented Jan 16, 2013

This is a simpler example to http://ardesco.lazerycode.com/index.php/2012/07/how-to-download-files-with-selenium-and-why-you-shouldnt/

for those that don't need the full features of that solution or have trouble integrating it into their code/framework.

@adiohana
Copy link

adiohana commented Jan 2, 2018

How about multiple cookies?

@mopul
Copy link

mopul commented Apr 23, 2018

I handle multiple cookies in the following way:

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        Set<Cookie> cookies = driver.manage().getCookies();
        if (cookies != null) {
            StringBuilder cookiesString = new StringBuilder();
            for (Cookie cookie : cookies) {
                cookiesString.append(cookie.getName()).append("=").append(cookie.getValue()).append("; ");
            }
            connection.setRequestProperty("Cookie", cookiesString.toString());
        } 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment