How to download a pdf file in chrome using selenium webdriver

0
0

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

  • You must to post comments
0
0

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
)
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website