How to download any file and save it to the desired location using Selenium Webdriver

You won't be able to access the save dialog box. That's controlled by the OS. The only thing you're really going to be able to do is set the default download location for the browser and allow it to automatically download the files. Then check the file in Java.

You should check this answer from this previous SO question. Basically when setting up your Firefox profile you add a call to set the property browser.helperApps.neverAsk.saveToDisk to a comma separated list of MIME types to always download:

firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

See this Mozilla KB FAQ article on About:configs.

UPDATE It looks like this may now be possible see this answer in another question


The Cancel/Save dialogue popup may be appearing because the site is sending you a different MIME type.

Check the actual header content.

Using the firefox built in Developer tools, Right click to inspect the element/download link your posting then take a look at the Network monitor to see the ContentType header value returned.. That would be the one you want to use..

enter image description here

Set your profile settings accordingly

 firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                     "application/octet-stream,text/csv");

I was expecting "text/csv" however got "application/octet-stream" once that was added to the accepted types list it all worked as expected, No popups


One potential solution is to obtain the URL for the file via Selenium, create a (non-Selenium) connection, copy Selenium's cookies to the connection (if necessary), and download the file. Most languages have APIs (or libraries) for performing HTTP requests. For example, to accomplish this in Java, you could use URL.openConnection():

String link = linkElement.getAttribute("href");
URL url = new URL(link);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");

You may need to copy over the cookies that Selenium has in order to imitate the Selenium user (for example if you are testing a website that requires a sign-in).

Set<Cookie> cookies = webDriver.manager().getCookies();
String cookieString = "";

for (Cookie cookie : cookies) {
    cookieString += cookie.getName() + "=" + cookie.getValue() + ";";
}

httpURLConnection.addRequestProperty("Cookie", cookieString);

Then you can use HttpURLConnection.getInputStream() to write the file contents to your preferred location.

try (InputStream in = httpURLConnection.getInputStream()) {
    Files.copy(in, new File("/path/to/file.ext").toPath(),
        StandardCopyOption.REPLACE_EXISTING);
}

Although, this method would be different for different programming languages, it works the same for all browsers.


I think you are looking for something like this

//common to all the cases
FirefoxProfile prof = new FirefoxProfile();

//Case:1 - Use this case to set download this code to your browser's default location
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//Case:2 - Download file to Desktop
//prof.setPreference("browser.download.folderList", 0);
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//Case:3 - Download to custom folder path. Replace d:\\selenium with your Download Location 
prof.setPreference("browser.download.dir","D:\\selenium\\");
prof.setPreference("browser.download.folderList", 2);
prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//This will work for all cases mentioned above
WebDriver driver = new FirefoxDriver(prof);
driver.get("http://docs.seleniumhq.org/download/");
driver.findElement(By.xpath("//tr[1]/td[4]/a[text()='Download']")).click();