There are situations where we need to
- Download files from browser,
- Save in a specified folder on hard disk.
We will use ChromeOptions to set browser preferences [much simpler way]
- Disable file save dialog
- set download path
Download the Chrome driver with respect to your browser version from https://chromedriver.chromium.org/downloads
public class Test { static WebDriver driver; public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","/Users/uname/sel/chromedriver"); String downloadFilepath = "/Users/uname/sel/"; 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(); options.setExperimentalOption("prefs", chromePrefs); options.addArguments("--test-type"); options.addArguments("--disable-extensions"); //to disable browser extension popup options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); driver = new ChromeDriver(options); driver.get("http://www.seleniumhq.org/download/"); driver.findElement(By.linkText("32 bit Windows IE")).click(); } }
File will be downloaded under sel folder.
The above specified path is for Mac OS, if you are using windows, then you can change the path accordingly
System.setProperty("webdriver.chrome.driver","C:\\Users\\uname\\sel\\chromedriver.exe"); String downloadFilepath = "C:\\Users\\uname\\sel";
Note – Selenium version 3.10 onwards – ChromeDriver(cap) is deprecated, need to use only ChromeOptions
For older versions of selenium < 3.10
We will be taking help of ChromePreferences, ChromeOptions and DesiredCapabilities of Chrome in selenium.
Find below code to download files in Chrome browser:
public class DownloadChromeFile {
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");
options.addArguments("--disable-extensions"); //to disable browser extension popup
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();
}
}
Find the downloaded file under the specified folder – c:\download
If you want to download files on firefox browser, then refer here
For demo, watch here
By using Robot class, we can achieve this easily…
if Save Button is Highlighting in Download popup:
Robot r=new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
why to use third party….its not a good way to code bro….when u can do using only selenium itself
Hello, I tried exactly the same solution and it’s showing me the Save window. Then I sued the Robot key event and it’s downloading to the user default download folder and NOT to the custom download directory.
Yes, same for me
Line 17 is empty because line 11 is instantiated but never assigned.
Chrome doesnot launch save window….mozilla does…this code is for chrome and not mozilla……mozilla has diff code……I tried this and this is working absolutely fine for me…..i opened my folder that i gave path for downloading……..it downloaded in that particular folder itself
Hi,
Each time I open Chrome “Ask where to save each file before downloading” is disabled, I have to manually set it on.
I want to know how to set it On through Selenium chrome options / any other method.
I am using Python and Selenium
Thanks