Download files in Chrome browser using selenium WebDriver

There are situations where we need to download files from browser, and save in a specified folder on hard disk.

Find below code to download files in Chrome browser:

public class DownloadXL {
       public static void main(String[] args) {
       System.setProperty("webdriver.chrome.driver","./chromedriver.exe");
       String downloadFilepath = "c:/download";

  HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
  chromePrefs.put("profile.default_content_settings.popups", 0);
  chromePrefs.put("download.default_directory", downloadFilepath);
  ChromeOptions options = new ChromeOptions();
  HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
  options.setExperimentalOption("prefs", chromePrefs);
  options.addArguments("--test-type");
  DesiredCapabilities cap = DesiredCapabilities.chrome();
  cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
  cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
  cap.setCapability(ChromeOptions.CAPABILITY, options);
  driver = new ChromeDriver(cap);  
                driver.get("http://www.seleniumhq.org/download/");
                driver.findElement(By.linkText("32 bit Windows IE")).click();
        }
} 

No comments:

Post a Comment