Skip to main content

How to Download File using WebDriver

When we download file from a web page, it asks for confirmation to download along with the download location. For automating this download file process, we have to set some preferences based on the browser used.
In this article, we will see file download for Firefox and Chrome separately.

Download File using FirefoxDriver

For firefox, we have to set the below firefox profile preferences.
  • browser.download.dir – The download directory
  • browser.download.folderList – It has to be set to 2 to make firefox use custom download directory (the above given download directory). If value is 0, it downloads the file to user’s desktop and if its 1, it uses the default Downloads folder.
  • browser.download.manager.showWhenStarting – Whether to show the download manager when starting download
  • browser.helperApps.neverAsk.saveToDisk – Never ask for saving to disk for the given file types.
Using the above preferences, we can create the profile and initialize the FirefoxDriver.
1
2
3
4
5
6
7
8
9
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.dir", "D:\\Custom\\Download\\Directory");
profile.setPreference("browser.download.folderList", 2);
// File type of the downloaded file
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference( "pdfjs.disabled", true );
// Initialize webdriver using the profile
WebDriver driver = new FirefoxDriver(profile);

Download File using Chrome

The method is similar for chromedriver in which we set the ExperimentalOptions preferences in ChromeOptions as shown below.
Preferences Set
  • download.default_directory – The download directory
  • profile.default_content_settings.popups – To disallow popups
1
2
3
4
5
6
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", "D:\\Downloads\\Directory");
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
We will be covering How to Import Files using WebDriver in a future post.

Comments

Popular posts from this blog

Reporting in Selenium

How do you Handle reporting in your project ? Method 1 : Maven - Testng - Surefire report The project will be created in Maven with  SureFire Plugin  and  SuiteXmlFile  tag in the POM.xml . The testng.xml will be called in the POM.            <plugin>                 <groupId>org.apache.maven.plugins</groupId>            <artifactId>maven-surefire-plugin</artifactId>            <version>2.18.1</version>                 <configuration>                     <suiteXmlFiles>                         <suiteXmlFile>                             ...

ExtentReports in Selenium Webdriver

ExtentReports in Selenium Webdriver What is ExtentReport? ExtentReports  is a HTML reporting library for Selenium WebDriver for Java which is extremely easy to use and creates beautiful execution reports. It shows test and step summary, test steps and status in a toggle view for quick analysis Download Download the jar below: Download ExtentReports 1.4 (1623)    Snapshot of Extent report After Executing the Script   Program Steps:  We are going to write three different testcases. Pass Warning Fail TestCase with Pass Result Navigate to http://www.guvi.in Click on Sign-in Enter the credientials Check the URL is correct or not after login   TestCase with Warning Result Verify with the Wrong URL (static String Afterloginfail="http://www.guvi.in/ ")    TestCase with fail Result Click on Menu Select Tech Challenges Verify With wrong URL. Source Code: import  java.io.File; import  j...

Handling Modal Dialog Window in Selenium Webdriver

Handling Modal Dialog Window in Selenium Webdriver In this tutorial we are going to see how to handle " Modal Dialog Window "  using Selenium Webdriver Disadvantage using Selenium Use Can't handle elements which is inside  Modal Dialog Window. Using Robot Class Using Robot Class we can handle  Modal Dialog Window. In this tutorial Open  http://vodkabears.github.io/remodal/# Click on  Show  button In the  Modal Dialog Window.  Click on Follow button  Source Code : import java.awt.AWTException; import java.awt.Robot; import java.awt.Toolkit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import java.awt.datatransfer.StringSelection; import java.awt.event.KeyEvent; public class robot_demo { public static void main(String[] args) throws InterruptedException, AWTException { WebDrive...