There are situations where we need to
- Download files from browser,
- Save in a specified folder on hard disk.
We will use browser preferences to [much simpler way]
- Disable file save dialog
- set download path
Download the latest geckodriver for firefox from https://github.com/mozilla/geckodriver/releases
We will use FirefoxOptions to set browser preferences
Code implementation
ublic class FileDownloadFireFox { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "/Users/uname/sel/geckodriver"); String downloadFilepath = "/Users/uname/sel/"; FirefoxOptions options = new FirefoxOptions(); options.setAcceptInsecureCerts(true); options.addPreference("browser.download.folderList", 2); options.addPreference("browser.helperApps.alwaysAsk.force", false); options.addPreference("browser.download.dir", downloadFilepath); options.addPreference("browser.download.defaultFolder",downloadFilepath); options.addPreference("browser.download.manager.showWhenStarting", false); options.addPreference("browser.helperApps.neverAsk.saveToDisk","multipart/x-zip,application/zip,application/x-zip-compressed,application/x-compressed,application/msword,application/csv,text/csv,image/png ,image/jpeg, application/pdf, text/html,text/plain, application/excel, application/vnd.ms-excel, application/x-excel, application/x-msexcel, application/octet-stream"); FirefoxDriver driver = new FirefoxDriver(options); driver.get("http://www.seleniumhq.org/download/"); driver.findElement(By.linkText("32 bit Windows IE")).click(); } }
File will be downloaded under specified location, the path specified in above code is for Mac OS, if you are using windows OS, the use
System.setProperty("webdriver.gecko.driver","C:\\Users\\uname\\sel\\geckodriver.exe"); String downloadFilepath = "C:\\Users\\uname\\sel";
For older versions of selenium < 3.10 , follow here
We will be using FirefoxProfile class to set certain preferences and download file from mozilla browser to our local hard disk.
Refer setPreferences for firefoxProfile.
Code implementation –
public class DownloadFile_FireFox { public static void main(String[] args) { FirefoxProfile profile= new FirefoxProfile(); //Accept SSL certificate errors profile.setAcceptUntrustedCertificates(true); profile.setAssumeUntrustedCertificateIssuer(true); //Download a file in FF browser profile.setPreference("browser.download.folderList", 2); profile.setPreference("browser.helperApps.alwaysAsk.force", false); profile.setPreference("browser.download.dir", "C:\\Downloads"); profile.setPreference("browser.download.defaultFolder","C:\\Downloads"); profile.setPreference("browser.download.manager.showWhenStarting", false); //Set MIME types profile.setPreference("browser.helperApps.neverAsk.saveToDisk","multipart/x-zip,application/zip,application/x-zip-compressed,application/x-compressed,application/msword,application/csv,text/csv,image/png ,image/jpeg, application/pdf, text/html,text/plain, application/excel, application/vnd.ms-excel, application/x-excel, application/x-msexcel, application/octet-stream" ); FirefoxDriver driver = new FirefoxDriver(profile); driver.get("http://the-internet.herokuapp.com/download"); Thread.Sleep(2000); driver.findElement(By.linkText("some-file.txt")).click(); } }
For MIME type or media type, please refer below links –
Make sure MIME type should match to the file type we are downloading from browser.
If you want to download files on Chrome browser, then refer here
For demo, watch here
Thank you so much.
This script is worked for me.