Not Able to download file | Selenium Forum
M
Posted on 18/01/2016
Hi,

I am using below code but not able to download file. In my understanding this line is not working

profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword,application/x-rar-compressed,application/octet-stream,application/csv,text/csv");

I am getting pop up asking for ok to download which i should not get.

Below is the code. Please help me in this regard

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class Downloadfiles {

public static void main(String[] args) {
FirefoxProfile fr= new FirefoxProfile();
fr.setPreference("browser.download.folderList", 2);
fr.setPreference("browser.download.dir", "F:\\downloads");
fr.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword");

WebDriver dr= new FirefoxDriver(fr);
dr.get("http://qtpselenium.com/test/testdownload.php");
dr.findElement(By.xpath("html/body/a[1]")).click();

}

}

M
Replied on 18/01/2016

use this test code


[code:35vr2qfv]package com.soapuitutorial.propertie;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class DownloadWithoutPrompt {
// http://stackoverflow.com/questions/34271396/how-to-handle-downloading-xlsx-file-in-firefox-using-webdriver-where-window-pop
public static void main(String[] args) {

FirefoxProfile profile = new FirefoxProfile();

profile.setPreference("browser.download.folderList", 2);

profile.setPreference("browser.download.manager.showWhenStarting",
false);

profile.setPreference("brower.download.dir", "C:\\temp"); // path of

profile.setPreference(
"browser.helperApps.neverAsk.saveToDisk",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");

profile.setPreference("browser.download.manager.showWhenStarting",
false);

profile.setPreference("browser.download.manager.alertOnEXEOpen", false);

WebDriver driver = new FirefoxDriver(profile);
driver.get("http://qtpselenium.com/test/testdownload.php");
driver.findElement(By.xpath("html/body/a[1]")).click();
driver.findElement(By.xpath("html/body/a[2]")).click();
driver.findElement(By.xpath("html/body/a[4]")).click();

}

}
[/code:35vr2qfv]