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."
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
)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With