简体   繁体   中英

How to download a 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 above code but its not working

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." 自动打开pdf

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
    )

您可以加载此页面并使用 selenium 导航更改设置:

chrome://settings/content/pdfDocuments

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);

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.

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

在此处输入图片说明

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM