Selenium Firefox Download PDF

Goal:

Download online PDF file using Firefox. 

Problem:

By default, Firefox will open the online PDF in the built-in PDF viewer.

Solution:

The following C# code will create a new Firefox profile and use it with selenium.

   1: FirefoxProfile profile = new FirefoxProfile();

   2:  

   3: // custom folder 

   4: profile.SetPreference("browser.download.folderList", 2); 

   5:  

   6: // download folder location

   7: profile.SetPreference("browser.download.dir", "C:\\XXXX");

   8:  

   9: // for PDF, save to disk without asking

  10: profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

  11:  

  12: // disable Firefox's built-in PDF viewer 

  13: profile.SetPreference("pdfjs.disabled", true); // disable Firefox's built-in PDF viewer 

  14:  

  15: var browserDriver = new FirefoxDriver(profile);

  16:  

  17: // save an online PDF 

  18: browserDriver.Navigate().GoToUrl(http://static.mozilla.com/moco/en-US/pdf/mozilla_privacypolicy.pdf);

Useful tip:

You can type “about:config” in Firefox browser address and create/search/view all these preference settings.

Other Approaches:

You can manually configure via Firefox’s Option –> Application.  Then you locate and load the profile from local disk..

Reference:

http://yizeng.me/2014/05/23/download-pdf-files-automatically-in-firefox-using-selenium-webdriver/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s