Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I have to perform following task using Selenium Webdriver given below.

  1. Click on any link/button that starts downloading any file (filetype may be anything image, pdf, jar, etc)
  2. Click on the "Save" on popup if appeared (e.g. in case of http://selenium.googlecode.com/files/selenium-server-standalone-2.33.0.jar)
  3. Give the desired location to save that file.

Can anyone share, how can we implement this using Java?

1 Answer

0 votes
by (62.9k points)

You can set Firefox browser's preference to save a file automatically, and not let the download window popup. Then you will be able to just grab the file, and it'll start getting downloaded.

So, something like this:

FirefoxProfile fxProfile = new FirefoxProfile();

 

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

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

fxProfile.setPreference("browser.download.dir","c:\\mydownloads");

fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");

 

WebDriver driver = new FirefoxDriver(fxProfile);

driver.navigate().to("http://www.foo.com/bah.csv");

and given that you now have the download directory, it will not ask you to save the file, and no download manager would appear, as automation from this point would be straightforward.

I hope this helps!

31k questions

32.9k answers

500 comments

692 users

Browse Categories

...