Save PDF instead of opening in Selenium

Code was used that will always download PDF. Since then, he began to open PDF inside the browser. The same thing happens for 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 open PDF instead of saving.

Any ideas?

+4
source share
2 answers

I can show you how we did it in Ruby, and I hope you can translate it according to your Java code (?). The main thing is to find out which preference keys are installed.

 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 displays preference strings such as "plugins.plugins_disabled", "download.prompt_for_download" and "download.default_directory"

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

+1
source

Newer solution:

  • Open chrome

  • Go to: plugins

  • Click the "Disable" link in the Chrome Pdf Viewer plugin

  • Do whatever you need.


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

C # example to get image with 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) { } } 
0
source

All Articles