Error while Downloading Files | Selenium Forum
M
Posted on 13/03/2016
Hi Ashish,

I am downloading file using selenium as you described in 15.6 video.Its working but only 1 file "xls" is downloaded ,"Word Doc" and "XLSX file " are not getting download in FF. Can you please help me on the same and correct me where I am wrong. And one doubt is in my mind In IE if I want to download all files present on the "http://qtpselenium.com/test/testdownload.php" webpage I will need initializes Robot object separately for each file or something else? I am using below mentioned code.I have attached screenshot for your reference. Kindly help me on the same.

Thanks in advance.


import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

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


public class Downloading_File {


public static void main(String[] args) throws InterruptedException {

FirefoxProfile profile =new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);//0(desktop),1(downloads folder),2(specified directory)
profile.setPreference("browser.download.manager.showWhenStarting", false); //prevent download manager window
profile.setPreference("browser.download.dir", "E:\\New folder");//Path of the folder Where we are downloading file
//profile.setPreference("browser.helperApps.neverAsk.openFile" ,
//"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.msexcel,image/png.image/jpeg,text/html,text/plain,application/msword,application/xml,application/vnd.openxmlformats-officedocument.wordprocessing.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel");
profile.setPreference("browser.download.manager.alertOnEXEOpen", false); //Warn user attempting to open an EXE file
WebDriver driver=new FirefoxDriver(profile);
driver.get("http://qtpselenium.com/test/testdownload.php");
driver.findElement(By.xpath("html/body/a[1]")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("html/body/a[2]")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("html/body/a[4]")).click();
Thread.sleep(5000);

/*System.setProperty("webdriver.ie.driver","E:\\IEDriverServer.exe");
InternetExplorerDriver driver = new InternetExplorerDriver();
driver.get("http://qtpselenium.com/test/testdownload.php");
driver.findElement(By.xpath("html/body/a[1]")).click();

Robot rb= new Robot();
rb.keyPress(KeyEvent.VK_LEFT);
Thread.sleep(3000);
rb.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(3000);
rb.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(3000);
rb.keyPress(KeyEvent.VK_ENTER);
*/


}

}

M
Replied on 13/03/2016

Here, File Manager for worddoc is maximized but only xls file is downloaded.Please refer screenshot.


M
Replied on 13/03/2016

try this

[code:3fakck41]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:3fakck41]