0

i m navigating a website where i have a situation that when I m clicking on a button it should download the pdf....

I am using the latest version of chrome 60, selenium 3.4, chromedriver.

        HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("plugins.plugins_disabled", new String[] {"Chrome PDF Viewer"});
        chromePrefs.put("profile.default_content_settings.popups", 0);
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", chromePrefs);
        DesiredCapabilities cap = DesiredCapabilities.chrome();
        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        cap.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver driver = new ChromeDriver(cap);

I have also used the above code, but it doesn't work.

  • 1
    possible duplicate of https://stackoverflow.com/questions/31672897/how-to-download-a-pdf-file-in-chrome-using-selenium-webdriver – Shubham Jain Aug 16 '17 at 15:26
  • Possible duplicate of [Auto-download in firefox browser with java-selenium not working](https://stackoverflow.com/questions/45589571/auto-download-in-firefox-browser-with-java-selenium-not-working) – undetected Selenium Aug 17 '17 at 07:25

2 Answers2

7

What worked for me was adding:

chromePrefs.put("plugins.always_open_pdf_externally", true);

I hope this helps

user1211
  • 1,507
  • 1
  • 18
  • 27
Cody Kutac
  • 93
  • 8
  • This just open the Acrobad Reader for me. While it does not open the preview in Chrome it does not skip opening the PDF at all – SirHawrk Jun 07 '21 at 14:22
0
/*ChromeOptions options = new ChromeOptions();

/*use to disable the "Chrome is handled by automation script" pop up.*/

options.addArguments("disable-infobars");  

/* use to start the Chrome browser as maximized.*/

options.addArguments("--start-maximized"); 

/* HashMap is use to disable the password saving pop up in Chrome browser.*/

Map<String, Object> prefs = new HashMap<String, Object>(); 

prefs.put("credentials_enable_service", false);

prefs.put("profile.password_manager_enabled", false);

options.setExperimentalOption("prefs", prefs);

/*Download File store in Download folder*/

prefs.put("profile.default_content_settings.popups", 0);

prefs.put("download.default_directory", downloadFilesPath);

prefs.put("plugins.always_open_pdf_externally", true);

HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();

options.setExperimentalOption("prefs", prefs);

options.addArguments("--test-type");

options.addArguments("--disable-extensions"); //to disable browser extension 

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);*
Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
  • 1
    Please consider formatting your post. This is very difficult to understand as it is. – Thom A Feb 12 '18 at 14:40
  • Code only answers are not as helpful as ones with discussion about why the code will fix the problem. Please edit your answer to add more detail. – Jess Bowers Feb 12 '18 at 15:13