Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File download using selenium

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.

like image 344
Luixv Avatar asked Apr 09 '26 07:04

Luixv


1 Answers

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.

like image 56
Luixv Avatar answered Apr 11 '26 21:04

Luixv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!