6

There was a code I use that would always download the PDF. Since recently it started to open the PDF inside the browser. The same happens for both chrome and firefox.

In chrome I already tried:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-extensions"));
driver = new ChromeDriver(capabilities);

And in firefox I tried:

FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser. download. manager. useWindow",true);
firefoxProfile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf");
firefoxProfile.setPreference("browser.download.dir","C:\\Documents and Settings\\xxxx\\My Documents\\Downloads");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf;text/plain;text/csv");
firefoxProfile.setPreference("pdfjs.disabled", true);
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force",false);
firefoxProfile.setPreference("plugin.scan.plid.all",false);
firefoxProfile.setPreference("plugin.scan.Acrobat","99.0");

But still, both browsers are opening the PDF instead of saving it.

Any ideas?

4

3 Answers 3

5

Basically change the options like below:

// this will make automatically download to the default folder.
chromeOptions.AddUserProfilePreference("plugins.always_open_pdf_externally", true);

C# Selenium Saving pdf page

1
  • You are welcome, wind please Up Vote if help. Thank you.
    – Haryono
    Aug 28, 2018 at 4:25
1

I can show you how we did it in Ruby, and hopefully you can translate it to fit your Java (?) code. The main thing is figuring out which preference keys to set.

Capybara.register_driver :selenium_chrome_downloads do |app|
  prefs = {
    plugins: {
      plugins_disabled: ['Chrome PDF Viewer']
    },
    download: {
      prompt_for_download: false,
      default_directory: 'desired/download/path'
    }
  }
  Capybara::Selenium::Driver.new(app, browser: :chrome, prefs: prefs)
end
Capybara::Session.new(:selenium_chrome_downloads)

This maps to preference strings like "plugins.plugins_disabled", "download.prompt_for_download", and "download.default_directory"

Helpful docs: https://sites.google.com/a/chromium.org/chromedriver/capabilities (mostly Java) https://code.google.com/p/selenium/wiki/RubyBindings (for Ruby)

0
0

Newer solution:

  1. Open chrome

  2. Go to about:plugins

  3. Click on "Disable" link in Chrome Pdf Viewer plugin

  4. Do all steps what you need.


you can create web-request and save response to the file

c# example to get image from url:

string webPath = "http://www.someasress.com/asdf.png";

                if (webPath != string.Empty)
                {
                    try
                    {
                        System.Net.WebRequest request =
                            System.Net.WebRequest.Create(webPath);

                        System.Net.WebResponse response = request.GetResponse();

                        System.IO.Stream responseStream = response.GetResponseStream();

                        Bitmap bitmapImg = new Bitmap(responseStream);

                        return bitmapImg;
                    }
                    catch (System.Net.WebException)
                    {
                    }
                }
2
  • Actually there is no URL of the file, it is just received after the submit. And it is dinamic as well. Aug 27, 2015 at 4:54
  • @user3905599, added another solution Aug 27, 2015 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.