For the firefox headless use below code.
//setting up browser driver exe path in System class
System.setProperty("webdriver.gecko.driver","C:\\Drivers\\geckodriver.exe");
//create object of FirefoxProfile class and setting up browser preferences
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.dir","C:\\Users\\Downloads");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/vnd.ms-excel");
//creating object of FirefoxOptions class and passing it the profile object
FirefoxOptions option = new FirefoxOptions();
option.addArguments("--private");
option.setProfile(profile);
//creating driver object and passing it option reference variable
WebDriver driver = new FirefoxDriver(option);
//the usual stuff
driver.get("http://spreadsheetpage.com/index.php/file/C35/P10/");
driver.findElement(By.linkText("smilechart.xls")).click();
driver.close();
Note1: Sometimes for certain browser and their specific version this above approch might not work then in that case you can use HttpUrlConnection java class or any third party utility like wget to download files.
Note2: Sometimes it takes few seconds for downloading to start and if in your next line of code you are closing browser or closing the session then file will not download then in that case you have to put a checkpoint to check existence of downloaded file along with checkpoint which checks size of file then close browser.
Save Water | Save Energy | Save Earth
Stop Pollution | Stop Plastic
Spread Peace
it not working for google chrome
ReplyDelete