How to download a file (.zip or .pdf) in Google Chrome or Mozilla Firefox browser using Selenium WebDriver?

How to download a file (.zip or .pdf) in Google Chrome or Mozilla Firefox browser using Selenium WebDriver?

Hi Brett,

you can write the following lines of code to download any file in Chrome or Mozilla using Selenium WebDriver:

import java.util.HashMap;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;


public class DownloadPDF {


static WebDriver driver;


public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", chrome_driver_path);

String downloadFilePath = "C:\\Users\\Prati_R\\Downloads\\PDFDownloads";

HashMap<String, Object> chromePref = new HashMap<String, Object>();

chromePref.put("profile.default_content_settings.popups", 0);

chromePref.put("download.default_directory", downloadFilePath);

ChromeOptions options = new ChromeOptions();

options.setExperimentalOption("prefs", chromePref);

driver = new ChromeDriver(options);

driver.get("https://www.seleniumhq.org/download/");

driver.findElement(By.xpath("//*[@id=\"mainContent\"]/p[7]/a[1]")).click();


Thread.sleep(3000);

driver.close();

}

}