How to download pdf file in chrome using selenium webdriver

I want to download pdf in chrome using selenium.

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + System.getProperty("file.separator") + "BrowserDrivers" + System.getProperty("file.separator") + "chromedriver.exe"); String downloadFilepath = "C:\\Users\\Vinod\\Downloads"; HashMap<String, Object> chromePrefs = new HashMap<String, Object>(); chromePrefs.put("profile.default_content_settings.popups", 0); chromePrefs.put("download.default_directory", downloadFilepath); //Save Chrome Opions ChromeOptions options = new ChromeOptions(); HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>(); options.setExperimentalOption("prefs", chromePrefs); options.addArguments("--test-type"); DesiredCapabilities cap = DesiredCapabilities.chrome(); cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap); cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); cap.setCapability(ChromeOptions.CAPABILITY, options); driver = new ChromeDriver(cap); driver.get(url); 

I tried the code but didn't work

+1
source share
4 answers

Since chrome 57 automatic PDF viewing no longer works as a plugin, there is now a setting that you can change to make this work. In fact, you can verify the prefix name by checking the chrome privilege dialog box in the "Content Settings" section, which says "Open PDF files in PDF application for viewing by default." automatic pdf open pref

You can set this value to false to avoid automatically previewing pdf, for example (Ruby example):

 caps = Selenium::WebDriver::Remote::Capabilities.chrome( "chromeOptions" => { 'args' => ['disable-gpu', "--window-size=1920,1080"], prefs: { "download.prompt_for_download": false, "download.directory_upgrade": true, "plugins.always_open_pdf_externally": true, "download.default_directory": DownloadHelpers::PATH.to_s } } ) Capybara::Selenium::Driver.new( app, browser: :chrome, desired_capabilities: caps ) 
+2
source

You can download this page and change the setting using selenium navigation:

 chrome://settings/content/pdfDocuments 
+2
source

Do you use Adobe Acrobat / Adobe Reader to display PDF files? If so, this is probably the Abode behavior you need to change.

Here are the steps I should have taken:

  • Open Adobe Reader
  • Menu "Edit", "Settings"
  • Selcet Internet from the list of categories
  • Uncheck "Show PDF in browser"
  • Click OK

you can also disable the Adobe plugin in Chrome, which will force Chrome to download the PDF.

This is under chrome: // plugins.

but, as you said, its latest chrome browser, check if there is a view in chrome format in view mode if it is disabled.

  • Disable "Chrome PDF Viewer" or "Unmark" is always allowed to run the check box. Try both

enter image description here

0
source

You will need to add the instructions below to your chrome profile:

 chromePrefs.put("pdfjs.disabled", true); 

It appears that newer versions of browsers come with a built-in ability to display PDF files inside the browser. See this for more information, although it is for the Firefox profile, but still well-read.

Hope that solves your problem, otherwise you may need to lower the chrome to make it work. Let me know if you have any questions.

0
source

All Articles