How to download a pdf file in chrome using selenium webdriver

前端 未结 5 917
生来不讨喜
生来不讨喜 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
        )
    
    0 讨论(0)
  • 2020-11-30 14:54

    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);
    
    0 讨论(0)
  • 2020-11-30 15:00

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

    chrome://settings/content/pdfDocuments
    
    0 讨论(0)
  • 2020-11-30 15:04

    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.

    0 讨论(0)
  • 2020-11-30 15:09

    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

    0 讨论(0)
提交回复
热议问题