M L N RAO
M L N RAO

Reputation: 11

How to handle downloading a file in selenium webdriver?

I am new to selenium and I have been assigned a task to implement below mentioned scenario.

Scenario : For the given URL, I need to enter into one web page which is having multiple links. and the links may have either pdf or HTML files. I need to just download the files by clicking that links and it should managed by entering the links reference in excel sheets.

So my task having two challenges:
1) how to handle XL sheet entered values as inputs.
2) how to download a pdf files using selenium

Please help me finding in any site, where i can download a file

Thanks

Upvotes: 1

Views: 17730

Answers (5)

Yury Staravoitau
Yury Staravoitau

Reputation: 175

After some local investigation, how to download pdf file in firefox without any Save As popup, I found the minimum required preference in firefox profile:

profile.setPreference("pdfjs.disabled", true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream;application/pdf");

of cource you can some additional preference. It works in Firefox 45-46 versions.

Upvotes: 0

Shubham Jain
Shubham Jain

Reputation: 17593

Apache POI. http://www.toolsqa.com/selenium-webdriver/data-driven-testing-excel-poi/

IF you are using Firefox

Use firefox profile to download your files. This profile skip that dialoge box of firefox. In line:-

   pro.setPreference("browser.downLoad.folderList", 0);

The value of browser.download.folderList can be set to either 0, 1, or 2. When set to 0, Firefox will save all files downloaded via the browser on the user's desktop. When set to 1, these downloads are stored in the Downloads folder. When set to 2, the location specified for the most recent download is utilized again.

Firefox profile code that you need to implement :-

        FirefoxProfile pro=new FirefoxProfile();
        pro.setPreference("browser.downLoad.folderList", 0);
        pro.setPreference("browser.helperApps.neverAsk.saveToDisk", "Applications/zip");
        WebDriver driver=new FirefoxDriver(pro);
        driver.get("http://selenium-release.storage.googleapis.com/2.47/selenium-java-2.47.1.zip");

Hope it will help you :)

Upvotes: 1

djangofan
djangofan

Reputation: 29689

This question has been asked before a few times but I use MetaModel Apache api for read and write to Excel. It allows me to query Excel using a SQL like syntax. I made a sample project of this in my GitHub account, if you can find it.

Also, for downloading files using a Selenium test I simply do this:

   1.  Use selenium to get the file download hyperlink.
   2.  Use Apache HttpUtils to download the file.  Don't use the browser to do it.

Upvotes: 0

user3603856
user3603856

Reputation: 39

Try below code

FirefoxProfile profile = new FirefoxProfile();
    String path = "D:\\Downloads_sel";
    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.dir", path);
    profile.setPreference("browser.download.alertOnEXEOpen", false);
    profile.setPreference("browser.helperApps.neverAsksaveToDisk", "application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel");
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.download.manager.focusWhenStarting", false);
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.closeWhenDone", false);
    profile.setPreference("browser.download.manager.showAlertOnComplete", false);
    profile.setPreference("browser.download.manager.useWindow", false);
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
    profile.setPreference("pdfjs.disabled", true);

    WebDriver driver = new FirefoxDriver(profile);

For complete MIME types list follow the link: http://qaautomationworld.blogspot.in/2014/02/file-downlaoding-using-selenium.html

Upvotes: 1

Gyorgy.Hegedus
Gyorgy.Hegedus

Reputation: 361

1a) You should try JExcelApi : http://jexcelapi.sourceforge.net/

1b) Another idea is to use Apache POI. You can find examples: http://poi.apache.org/spreadsheet/how-to.html#sxssf

2) To download PDF files you can try this Selenium code (written in Java) :

FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(profile); 

profile.setPreference("browser.helperapps.neverAsk.saveToDisk" , "application/octet-stream;application/pdf"); 
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.folderList", 2); 
profile.setPreference("browser.download.dir","C:\\savedPDFs");

When you click to a link (may have pdf file), the pdf file will be downloaded automatically into the given folder (without popup)

Upvotes: 0

Related Questions