• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How to Download files in Selenium Webdriver

How to Download files in Selenium Webdriver

March 20, 2015 by Mukesh Otwani 59 Comments

Hello Welcome to Selenium tutorial, today we will see How to Download files using Selenium Webdriver.

In previous post, we have seen how to upload files using robot class and upload files using AutoIT. Today we will see some different scenario we will see downloading files in Selenium.

Sometime in your application, you have to control some scenario in which you need to download some files by clicking on some link or some button.

Once you start download files/application, you will get one confirmation window, which will ask to save, file or cancel here Webdriver stuck because this is Window’s Pop up. Selenium can handle only Web browser automation not windows based application. Refer below screenshot for more information.

Here download files window is displayed if you try to inspect using firebug you will not get any locators for them.
How to Download files using Selenium

So using some browser settings (firefoxprofile) we can skip that confirmation window and we can continue with our script.

Here is some setting that we need to modify let’s see how to check these setting

How to Download files in Selenium Webdriver

1- Open Firefox browser and in url box type about:config and press enter

2- In Search bar type neverask and enter, here you will find some settings(refer below screenshot)
How to Download files using Selenium

Now you can see here value is blank so we need to mention which type of file it will not ask if download starts in case.

Note- In this post I am giving values for .exe file(application), in your case if you want to download pdf, excel file etc. you need to mention values (MIME type).

Here you can find their MIME type that will be the values for these settings.

http://www.sitepoint.com/web-foundations/mime-types-complete-list/

How to Download files in Selenium Webdriver

Step 1- Create a firefox Profile.

Step 2- set Preferences as per requirement.

Step 3- Open Firefox with firefox profile.

Let us implement the same through Script.

Note- This script will download Adobe Reader from Filehippo.com

package blog;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class DownloadFiles {

public static void main(String[] args) {
  
// Create a profile
FirefoxProfile profile=new FirefoxProfile();

// Set preferences for file type 
profile.setPreference("browser.helperApps.neverAsk.openFile", "application/octet-stream");
  
// Open browser with profile                   
WebDriver driver=new FirefoxDriver(profile);
  
// Set implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  
// Maximize window
driver.manage().window().maximize();
  
// Open APP to download application
driver.get("http://www.filehippo.com/download_adobe_reader");
  
// Click on download 
driver.findElement(By.xpath(".//*[@id='program-header']/div[4]/a[1]")).click();
    
 }

}

 

If you run above code files will be download automatically and download window will not appear 🙂

Please comment below if you have any issue in Selenium. Thanks for visiting my blog keep in touch.

Bye.

 

Filed Under: Advance Selenium Tagged With: Download file

Reader Interactions

Comments

  1. divya says

    November 10, 2020 at 8:01 PM

    Hi Mukesh,

    When i run my code it downloads .xls file but dont for xlsx .also it doesn’t give any error like element not found or anything . M working on chrome browser.is there a need to define mime type in chrome?kindly help.

    Reply
    • Mukesh Otwani says

      November 11, 2020 at 2:47 PM

      Hey Divya, for Chrome you don’t need to set any MIME type. You can just change the download directory and you can verify the same.

      Reply
  2. Anurag says

    May 31, 2019 at 5:46 PM

    How to do this in with ChromeDriver

    Reply
    • Mukesh Otwani says

      May 31, 2019 at 9:58 PM

      Hi Anurag,

      Try with below code
      Map prefsMap = new HashMap();
      prefsMap.put(“profile.default_content_settings.popups”, 0);
      prefsMap.put(“download.default_directory”, “Path of the download folder”);
      ChromeOptions option = new ChromeOptions();
      option.setExperimentalOption(“prefs”, prefsMap);
      WebDriver driver = new ChromeDriver(option);

      Reply
  3. Sanjay says

    May 24, 2019 at 12:22 PM

    This is very helpful and very easy to follow. Thanks for the post.

    Reply
    • Mukesh Otwani says

      May 24, 2019 at 2:36 PM

      Hi Sanjay,

      You’re welcome…:)

      Reply
  4. Rizwana says

    April 30, 2019 at 12:02 PM

    Hi Mukesh,

    I am getting an error at the line driver=new FirefoxDriver(profile); Error is ‘The Constructor FirefoxDriver(FirefoxProfile) is undefined’. Please help me to resolve this issue. I am using Firefox 62 and selenium 3.7.1

    Reply
    • Mukesh Otwani says

      April 30, 2019 at 2:08 PM

      Hi Rizwana,

      FirefoxProfile has to used with FirefoxOptions from version 3.6.0 onwards. Here is the link for more understandibility https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/firefox/FirefoxOptions.html

      Reply
      • shushma says

        June 27, 2019 at 3:03 AM

        Hi Mukesh,

        i was getting same error and also i have gone through your link error resolved. But its not clicking on save button. File is not downloading.

        my code is:

        FirefoxOptions options = new FirefoxOptions()
        .addPreference(“browser.startup.page”, 1)
        .addPreference(“browser.startup.homepage”, “https://filehippo.com/download_avast_antivirus/”);
        options.addPreference(“browser.helperApps.neverAsk.openFile”, “application/octet-stream”);

        Reply
        • Mukesh Otwani says

          June 28, 2019 at 12:11 AM

          Hi Shushma,

          Mozilla team is not interested to set mime type of application/octet-stream in browser profile because of security threat. You can check this link https://bugzilla.mozilla.org/show_bug.cgi?id=201546 for more understanding…

          Reply
          • shushma says

            June 28, 2019 at 2:00 AM

            Thank you..

  5. swagatika says

    April 23, 2019 at 12:58 AM

    Hi mukesh ,

    can you please clarifye ,

    in line number 16 , why you mentioned “application/octet-stream” is it a file type ?

    Reply
    • Mukesh Otwani says

      April 23, 2019 at 3:14 PM

      Hi Sangeetha,

      Hope this link will help you to understand it

      Reply
    • Mukesh Otwani says

      April 23, 2019 at 3:14 PM

      Hi Swagatika,

      Hope this link will help you to understand it

      Reply
  6. Dhanashree says

    April 4, 2019 at 11:51 AM

    Hi Mukesh ,
    How to handle pdf controls like (Zoom in , Zoom out ) . Can you please help me ? Can you share me java code for reference .

    Reply
    • Mukesh Otwani says

      April 4, 2019 at 2:28 PM

      Hi Dhanashree,

      Handling controls like zoom in / zoom out lies with api of native application which you are using to open pdf file. Its nowhere related to Selenium.

      Reply
  7. Divya Agnihotri says

    April 2, 2019 at 9:43 PM

    I am getting an error in following line
    WebDriver driver = new FirefoxDriver(profile);

    Error is “TThe constructor FirefoxDriver(FirefoxProfile) is undefined”
    Please help.

    Reply
    • Mukesh Otwani says

      April 3, 2019 at 12:23 AM

      Hi Divya,

      Which version of Selenium are you using?

      Reply
  8. manoj kumar sahoo says

    February 5, 2017 at 11:53 PM

    i am not able to save a file.could you please upload the video of it?

    Reply
    • Mukesh Otwani says

      February 6, 2017 at 11:41 AM

      Hi Manoj,

      I will post it soon.

      Reply
  9. Sowmyashree R says

    January 24, 2017 at 12:56 PM

    On clicking on Export in my application, I get a pop up asking to Open or Save file. How to open or save a file and check whether expected file exists, please explain…

    Reply
    • Sowmyashree R says

      January 24, 2017 at 12:57 PM

      Please explain for Chrome and IE

      Reply
      • Mukesh Otwani says

        January 24, 2017 at 4:04 PM

        I will upload soon.

        Reply
    • Mukesh Otwani says

      January 24, 2017 at 5:07 PM

      Hi Sowmya,

      Code will download the file then you can write java program to check whether file exist or not after downloading.

      Reply
      • Sowmyashree R says

        January 25, 2017 at 1:31 PM

        Thanks Mukesh, could you please share the code

        Reply
        • Mukesh Otwani says

          January 25, 2017 at 4:58 PM

          Hi Sowmyashree,

          Please keep patience, very soon I’ll post a video for this.

          Reply
  10. sonali says

    January 23, 2017 at 4:37 PM

    how to download files using chrome browser

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 4:52 PM

      Hi Sonali,

      I will upload soon.

      Reply
  11. Tarun says

    December 13, 2016 at 2:37 AM

    Hi Mukesh,

    I would like to change default download dir for Chrome? How do you set capabilities in browser?

    Following is my code. It sets default download path but it pop up dialog box to save file. Any help is appreciated.

    Map chromePrefs = new HashMap();
    chromePrefs.put(“profile.default_content_settings.popups”, 2);
    chromePrefs.put(“download.default_directory”, getFilePath());
    ChromeOptions options = new ChromeOptions();
    //Map chromeOptionsMap = new HashMap();
    options.setExperimentalOption(“prefs”, chromePrefs);
    options.addArguments(“–test-type”);

    capability = DesiredCapabilities.chrome();
    capability.setCapability(“chrome.switches”, Arrays.asList(“–start-maximized,–ignore-certificate-error”));
    capability.setCapability(ChromeOptions.CAPABILITY, options);
    //capability.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);

    Reply
    • Mukesh Otwani says

      January 11, 2017 at 3:22 PM

      is this fixed Tarun?

      Reply
  12. Lavina says

    November 9, 2016 at 8:31 PM

    Hi Mukesh,

    After clicking on Download icon, i am getting a popup window having options ‘open with’ , ‘save file’ & there are two options at the bottom of popup window having ‘OK’ or ‘Cancel’ option.
    I just want to click on download icon(Which i am able to do) but then i want to cancel it from popup & don’t want to open or save it.
    How do i achieve it?

    Reply
    • Mukesh Otwani says

      January 11, 2017 at 3:01 PM

      Hi Lavina is this fixed?

      Reply
  13. suresh says

    November 9, 2016 at 1:49 AM

    hii, mukesh ur teaching is excellent

    Reply
    • Mukesh Otwani says

      December 14, 2016 at 12:29 AM

      Thanks Suresh

      Reply
  14. shilpa says

    October 21, 2016 at 2:29 AM

    Hi Mukesh,

    I want to check “downloading pdf” files. But I have to write script with Ruby. And I dont have much experience in Ruby. Can you plz help there?

    Reply
    • Mukesh Otwani says

      November 6, 2016 at 10:19 AM

      Hi Shilpa,

      Not sure on Ruby 🙁

      Reply
  15. Sreenath says

    October 18, 2016 at 3:33 PM

    Hi Mukesh ,

    This code is for Mozilla Firefox, how to do this in Chrome/IE

    Reply
  16. Priyanka Chouhan says

    October 17, 2016 at 2:33 AM

    I becomes fan of urs..great way of explaining thing.. I m new to selenium..your post really helpful for me.. Thankyou

    Reply
    • Mukesh Otwani says

      October 18, 2016 at 2:25 PM

      Hey Priyanka,

      🙂 Your comment made my day. Thank you so much. keep in touch and let me know if any help required in Selenium.

      Reply
  17. Vasu says

    October 13, 2016 at 3:38 PM

    Hi Mukesh,
    Loved your way of explaining the things. 🙂
    here i’ve a doubt on downloading files
    If we follow the way of configuring all the MIME types we require in the Firefox browser, it never never pops up a download but how can we ensure whether file downloading happening or not?(There could be a chance of getting a bugs like below
    1.Even if selenium clicks no file gets downloaded. 2.There could be page redirection sometimes
    3. Redirects to an error page)
    So what i feel is like firing an robot class event on the buttons like ‘Ok’ or ‘cancel’ on the download window so that we can ensure file downloading happening.

    Reply
    • Mukesh Otwani says

      October 13, 2016 at 4:40 PM

      Yes Vasu correct and you can follow robot class as well. My main intention was to handle download option because it may stop the execution.

      Reply
  18. Suresh Sharma says

    October 10, 2016 at 4:06 PM

    Hi Mukesh,
    same is working fine. when i run via eclipse–>testng.xml–>RunasTestSuite but same script is not working with Jenkins.
    Moreover i created sikuli script for the same and skiuli script is also working fine with eclipse but not with jenkins.
    I am running jenkins as windows’ service.
    pls suggest!!

    Reply
    • Mukesh Otwani says

      October 12, 2016 at 11:47 AM

      Hey Suresh,

      Strange 🙁 Can you tell me what exception it generate while running via Jenkins.

      Note- if jenkins runs in headless mode then Sikuli won’t work.

      Reply
  19. Learner says

    May 17, 2016 at 3:20 PM

    The above code is not working for .pdf files. i have used

    profile.setPreference(“browser.helperApps.neverAsk.saveToDisk”, “application/pdf”);

    profile.setPreference(“browser.helperApps.neverAsk.openFile”, “application/pdf”);

    Everytime the pop up window appears to ask to save or open the file when eclipse open the browser, however when I open the browser manually and click on download the file it automatically download the file without any pop ups.

    Reply
    • Mukesh Otwani says

      May 19, 2016 at 4:29 AM

      Hi Kopal,

      Please use Sikuli for this now 🙂

      Reply
  20. divakar says

    April 19, 2016 at 8:41 AM

    Superb explanation!!!!!

    Reply
    • Mukesh Otwani says

      April 19, 2016 at 9:53 AM

      Thanks Divakar 🙂 Keep visiting.

      Reply
  21. Nitin says

    March 2, 2016 at 12:21 AM

    Beautiful!! Thanks for sharing the same!! 🙂

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 2:08 PM

      Thanks Nitin

      Reply
  22. vivek says

    March 1, 2016 at 1:20 PM

    thanks…

    Reply
    • Mukesh Otwani says

      March 1, 2016 at 11:59 PM

      Thanks Vivek

      Reply
  23. sivakrishna says

    January 2, 2016 at 8:26 PM

    hi mukesh

    i have already created webdriver object for my previous testscripts,so only for pdf download if i create another driver object like above is it good ?
    other wise how can i set the fire fox profile for the existing driver object.

    bcoz i am using jbehave with serenity.
    plz reply me asap…
    hope u understand my problem

    Thanks&Regards,
    Krishna

    Reply
    • Mukesh Otwani says

      January 2, 2016 at 10:28 PM

      Hi Siva,

      I would suggest add all the MIME type in firefox profile which are mostly used in application like pdf,.exe,word etc.

      Reply
  24. QualityAnalyst says

    December 25, 2015 at 5:20 PM

    I tried for downloading excel file but didn’t work.
    Here is my code,

    @Before
    public void setUp() throws Exception {

    FirefoxProfile profile=new FirefoxProfile();
    profile.setPreference(“browser.helperApps.neverAsk.saveToDisk”, “D://App_Downloads/x-excel”);

    driver=new FirefoxDriver(profile);
    baseUrl = “http://cschool.co”;
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

    @Test
    public void testCase02() throws Exception {
    driver.get(baseUrl + “/admin/”);
    driver.manage().window().maximize();

    driver.findElement(By.cssSelector(“button.saveButton”)).click();
    driver.findElement(By.linkText(“Download”)).click();
    Thread.sleep(3000);

    Robot object=new Robot();
    object.keyPress(KeyEvent.VK_ENTER);
    }
    Please help me out!

    Reply
    • Mukesh Otwani says

      December 26, 2015 at 1:06 AM

      Make the below changes and run again

      profile.setPreference(“browser.helperApps.neverAsk.saveToDisk”, “application/excel”);

      Reply
  25. sateesh says

    December 2, 2015 at 10:28 PM

    What is the mime type for .apk(Android APP Apk ) file or .ipa (IOS APP ipa)file
    ….here i’m trying to download the .apk (android apk file) file, i was stucked at download dialog box where i’m unable to click on save button.

    Reply
    • Mukesh Otwani says

      December 2, 2015 at 10:57 PM

      Hi Sateesh,

      try below MIME type application/vnd.android.package-archive

      Reply
  26. Omprakash says

    November 4, 2015 at 6:48 PM

    i have tried above process for Jpeg and tiff format files. but is not working

    Reply
    • Mukesh Otwani says

      November 5, 2015 at 1:31 AM

      Hi Omprakash,

      Have you changed the MEMI type of jpeg file because this is working code and I use this frequently in my application.

      Can u please check ur code again?

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?