Quality Testing

Quality is delighting customers

Download file and save it in desired location using selenium webdriver

I have used below code for download file and save it in desire location, but it is not working.

String dirpath = "D:\\Download\\";

FirefoxProfile prof = new FirefoxProfile();

prof.setPreference("browser.download.dir",dirpath);

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

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

prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;" + "text/csv");

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

prof.setPreference( "pdfjs.disabled", true );

driver = new FirefoxDriver(prof);

String url = "http://local.test.com";

driver.get(url);

driver.findElement(By.id("spanLabel0")).click();

driver.findElement(By.id("spanLabel0")).sendKeys("auto@qa.com");

driver.findElement(By.xpath("html/body/div[1]/div/div[1]/div[2]/div[3]/a")).click();

driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);

driver.findElement(By.linkText("Main")).click();


driver.findElement(By.linkText("Dashboard")).click();

Thread.sleep(5000);

driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);

new Select(driver.findElement(By.id("dropDownBox"))).selectByVisibleText("Last Month");

Thread.sleep(5000);

driver.findElement(By.xpath("//*[@id='spnCSV']/div")).click();

driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);

System.out.println("Download done without popup" +'\n'+'\n');

Can any one help me...

Views: 1567

Reply to This

Replies to This Discussion

//Try this

import
java.awt.Robot;

Robot
r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r
.keyRelease(KeyEvent.VK_ENTER);

Thanks for your reply.

I have tried with Robot API, it is not working.

Robot ojt = new Robot();

ojt.keyPress(KeyEvent.VK_DOWN);

ojt.keyPress(KeyEvent.VK_ENTER);

ojt.keyRelease(KeyEvent.VK_ENTER);

can you find out which key combination will select save file option?

Hi Member,

You can try the below code as it worked for me to download file to desired location :

//common to all the cases
FirefoxProfile prof = new FirefoxProfile();

//Case:1 - Use this case to set download this code to your browser's default location
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//Case:2 - Download file to Desktop
//prof.setPreference("browser.download.folderList", 0);
//prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//Case:3 - Download to custom folder path. Replace d:\\selenium with your Download Location
prof.setPreference("browser.download.dir","D:\\selenium\\");
prof.setPreference("browser.download.folderList", 2);
prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

//This will work for all cases mentioned above
WebDriver driver = new FirefoxDriver(prof);
driver.get("http://docs.seleniumhq.org/download/");
driver.findElement(By.xpath("//tr[1]/td[4]/a[text()='Download']")).click();

In case none of above approach works, please try with using any other website <i.e other than http://local.test.com> mentioned in your code.

Most of the automated testing services are using above approach to resolve such issues.

Hope this will solve your problem.

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2019   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service