How to download a pdf file in chrome using selenium webdriver

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
    )

Leave a Comment