Shankar Daitha
Shankar Daitha

Reputation: 243

How to download PDF file in to my framework folder using selenium

I am working on one application. In that application when we click on one button. A PDF file will be opened in a new pop up browser.

Now My requirement is to once I click on the button, instead of opening a new browser. We need to download the file into the framework folder.

"src/main/java/resources" folder.

I need a selenium code in java to support this.

I am using Chrome browser.

Upvotes: 0

Views: 372

Answers (1)

Pritam Maske
Pritam Maske

Reputation: 2760

Use this :

  //Create FireFox Profile object
 FirefoxProfile profile = new FirefoxProfile();

 //Set Location to store files after downloading.
 profile.setPreference("browser.download.dir", "src/main/java/resources");
 profile.setPreference("browser.download.folderList", 2);

 //Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
 profile.setPreference("browser.helperApps.neverAsk.saveToDisk", 
     "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"); 

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

 //Pass FProfile parameter In webdriver to use preferences to download file.
 FirefoxDriver driver = new FirefoxDriver(profile);  

If the above solution doesn't work then use following option :

  1. Open Developer Tools
  2. Go to Network
  3. Click on the link to download the pdf
  4. In the network panel, select the first request
  5. The mime type is the Content-Type from the response header: Download pdf
  6. And then use the above mentioned code of firefox profile

Upvotes: 1

Related Questions