How to download a pdf file in chrome using selenium webdriver
Asked Answered
O

6

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

Ology answered 28/7, 2015 at 9:53 Comment(2)
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?Vaccination
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 downloadingOlogy
A
6

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
    )
Anorthosite answered 16/5, 2017 at 13:8 Comment(0)
N
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);
Nell answered 28/10, 2019 at 21:35 Comment(0)
C
3

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

chrome://settings/content/pdfDocuments
Canzone answered 21/8, 2017 at 22:0 Comment(0)
S
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.

Studnia answered 28/7, 2015 at 12:34 Comment(1)
For firefox i'm using the same code i.e ff profile. I tried with that command also but no useOlogy
M
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

Maidamaidan answered 28/7, 2015 at 11:46 Comment(8)
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 IEOlogy
then you can do this when ever selenium scrip runs it should disable all extensions simply by your selenium code try this at link #14049651Maidamaidan
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 thisMaidamaidan
Fahad I refered that link I think it is for extensions and not for plugins, even though I tried it but no use.Ology
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 classOlogy
Can anyone please help me outOlogy
i dont see any other solution sorryMaidamaidan
its ok fahad i'm still trying on itOlogy
B
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)
Broadfaced answered 18/8, 2022 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.