How to download a pdf file in chrome using selenium webdriver

前端 未结 5 922
生来不讨喜
生来不讨喜 2020-11-30 14:33

I want to download pdf in chrome using selenium.

System.setProperty(\"webdriver.chrome.driver\", System.getProperty(\"user.dir\")  
               + System.         


        
5条回答
  •  眼角桃花
    2020-11-30 14:52

    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."

    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
        )
    

提交回复
热议问题