I am using selenium for testing a system. I have to download a text file. In order to download it directly I've created a Firefox profile which should avoid the Save / cancel
dialog... butthe dialog is still coming.
My code is as follows:
FirefoxProfile fxProfile = new FirefoxProfile();
fxProfile.setPreference("browser.download.folderList",2);
fxProfile.setPreference("browser.download.manager.showWhenStarting",false);
fxProfile.setPreference("browser.download.dir","c:\\tmp");
fxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/plain");
driver.findElement(By.id("link-download")).click();
I cannot find why the dialog is still opened. Any idea?
Thanks in advance.
I got it.
The Firefox profile has to be passed as a parameter when creating the driver as follows:
FirefoxProfile fxProfile = new FirefoxProfile();
fxProfile.setPreference("browser.download.folderList", 2);
fxProfile.setPreference("browser.download.manager.showWhenStarting", false);
fxProfile.setPreference("browser.download.dir","c:\\tmp");
fxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
setDriver(TestBench.createDriver(new FirefoxDriver(fxProfile)));
I wass creating the driver without parameters. Now it is working.
In my case, I need another Preference to make it working, it's also interesting to have Javascript Enabled.
File downloadDir = new File(DOWNLOAD_PATH);
FirefoxProfile fProfile = new FirefoxProfile();
fProfile.setAcceptUntrustedCertificates(true);
fProfile.setPreference("browser.download.dir", downloadDir.getAbsolutePath());
fProfile.setPreference("browser.download.folderList", 2);
fProfile.setPreference("browser.download.manager.showWhenStarting", false);
fProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
fProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/plain");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setJavascriptEnabled(true);
dc.setCapability(FirefoxDriver.PROFILE, fProfile);
driver = new FirefoxDriver(dc);
You Can Download All Files (Eg: .xls, .csv, .pdf)
I also face same problem in my application:
I got solution using Robot in java
following code i write to download all file
Thread.sleep(1000L);
//create robot object
Robot robot = new Robot();
Thread.sleep(1000L);
//Click Down Arrow Key to select "Save File" Radio Button
robot.keyPress(KeyEvent.VK_DOWN);
Thread.sleep(1000L);
// Click 3 times Tab to take focus on "OK" Button
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(1000L);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(1000L);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(1000L);
//Click "Enter" Button to download file
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(5000L);
System.out.println("Robot work Complete");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.