4

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 above code but its not working

Danielson
  • 2,605
  • 2
  • 28
  • 51
Vinod Kumar
  • 41
  • 1
  • 2
  • 4
  • 1
    Why is not it working? Where is the problem? Maybe some debugger use? Or error status of some function call ? What have you tried in order to fix the problem? – VP. Jul 28 '15 at 10:03
  • 1
    Actually when I click a button, pdf should be downloaded automatically, In previous browser versions it works properly. My browser was updated and the pdf is opening instead of downloading – Vinod Kumar Jul 28 '15 at 10:05

6 Answers6

5

Since chrome 57 the automatic pdf preview no longer works as a plugin, there's now a setting you can change to make this work. You can actually inspect the name of the pref by inspecting the chrome's own preference dialog, under "Content Settings" the flag that says "Open PDF files in the default PDF viewer application." automatic pdf open pref

You can set that to false to avoid automatic pdf preview, like this (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
    )
rogercampos
  • 1,676
  • 1
  • 10
  • 8
4

Here are the C# options for anyone working with .NET

var tsTimeout = new TimeSpan(0, 5, 0);

ChromeOptions chromeOptions = new ChromeOptions(); 
chromeOptions.AddUserProfilePreference("download.default_directory", _downloadFolder); 
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false); 
chromeOptions.AddUserProfilePreference("download.directory_upgrade", true); 
chromeOptions.AddUserProfilePreference("plugins.plugins_disabled", "Chrome PDF Viewer"); 
chromeOptions.AddUserProfilePreference("plugins.always_open_pdf_externally", true);

_driver = new ChromeDriver(CWebCrawler.WebCrawlerRootFolder, chromeOptions, tsTimeout);
windchaser
  • 141
  • 1
  • 5
3

You might load this page and change the setting using selenium navigations:

chrome://settings/content/pdfDocuments
Alexan
  • 8,165
  • 14
  • 74
  • 101
1

You would need to add below statement to your chrome profile:

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

It seems that newer versions of browsers are coming with built-in ability of displaying PDF files inside browser. Refer this for more information, though it is for firefox profile but still a good read.

Hope that solves your issue, else you may need to downgrade your chrome to make it work. Let me know if you have any queries.

Manu
  • 2,251
  • 19
  • 30
0

Are you using Adobe Acrobat/Adobe Reader to display the PDFs? If so, it is probably Abode's behavior you need to modify.

Here are the steps I had to take:

  1. Open Adobe Reader
  2. Edit menu, Preferences
  3. Selcet Internet from the Categories list
  4. Uncheck Display PDF in browser
  5. Press OK

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

It's under chrome://plugins.

but as you said its latest chrome browser then check is chrome PDF view visible in plugin if Yes disable it too.

  • Disable "Chrome PDF Viewer" or unmark always allowed to run check box try both

enter image description here

fahad
  • 389
  • 2
  • 12
  • Previously I done like that only by disabling the pdf viewer but after updating the browser i'm getting the problem even though the plugin is in disable mode. Second thing is if I do lik unchecking pdf browser in adobe i may get prob in IE – Vinod Kumar Jul 28 '15 at 11:53
  • then you can do this when ever selenium scrip runs it should disable all extensions simply by your selenium code try this at link http://stackoverflow.com/questions/14049151/how-to-disable-chrome-extension-in-selenium – fahad Jul 28 '15 at 11:54
  • 1
    you can try this use ROBOT class and press ctrl+S to save pdf and then hit Enter from robot that would save pdf in hard disk ? try this – fahad Jul 28 '15 at 12:01
  • Fahad I refered that link I think it is for extensions and not for plugins, even though I tried it but no use. – Vinod Kumar Jul 28 '15 at 12:09
  • And for FF IE I'm using sikuli and ff profiles. So I need to change the code in so many places to use robot class – Vinod Kumar Jul 28 '15 at 12:10
  • Can anyone please help me out – Vinod Kumar Aug 05 '15 at 05:36
  • i dont see any other solution sorry – fahad Aug 05 '15 at 07:16
  • its ok fahad i'm still trying on it – Vinod Kumar Aug 06 '15 at 12:32
0

The following python code worked for me in Chrome to download pdf by disabling the pdf viewer:

options = webdriver.ChromeOptions()
prefs = {"download.default_directory": chromeDownloadPath,
         "download.prompt_for_download": False,
         "download.extensions_to_open": "applications/pdf",
         "plugins.plugins_disabled": "Chrome PDF Viewer",
         "plugins.always_open_pdf_externally": True}
options.add_experimental_option("prefs", prefs)    
driver = webdriver.Chrome('chromedriver', options=options)
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Bibin Gangadharan
  • 1,393
  • 12
  • 13