Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 238
Vinod Kumar Avatar asked Dec 14 '22 12:12

Vinod Kumar


2 Answers

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
    )
like image 114
rogercampos Avatar answered Dec 18 '22 12:12

rogercampos


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);
like image 23
windchaser Avatar answered Dec 18 '22 10:12

windchaser