Banjaxx
Banjaxx

Reputation: 644

chrome webdriver - PDF documents downloading and not opening in new tab

I have found an issue whereby I will click on a link inside a webpage to a PDF document using google chrome. Previously, when I clicked on the link, the PDF document would load in a new tab. I would then perform some validation around the document.

Recently, the test has started to fail because when I click on the link to a PDF document, it downloads the PDF file rather than open it in a new tab. I'm assuming that the issue has recently occurred due to an update to chrome or the chrome driver.

If I manually check the link i.e not through the chrome driver instance then it will open in a new tab just fine. Has anyone encountered this issue?

Upvotes: 1

Views: 4917

Answers (3)

Sarah QA
Sarah QA

Reputation: 105

This works fine when we enable Chrome PDF Viewer plugin in the Profile preference:

HashMap<String, Object> plugin = new HashMap<String, Object>();
plugin.put("enabled", true);
plugin.put("name", "Chrome PDF Viewer");
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put("plugins.plugins_list", Arrays.asList(plugin));
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
ChromeDriver driver = new ChromeDriver(options);

Upvotes: 0

customcommander
customcommander

Reputation: 18891

Just solved a similar issue. Here's my JS code:

var webdriver = require('selenium-webdriver');
var chrome    = require('selenium-webdriver/chrome');

var options = new chrome.Options();
var builder = new webdriver.Builder();
var driver;

builder.withCapabilities(webdriver.Capabilities.chrome());

options = chrome.Options.fromCapabilities(builder.getCapabilities());
options.excludeSwitches('test-type','ignore-certificate-errors');

builder.setChromeOptions(options);

driver = builder.build();

driver.get('https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf');

// driver.quit();

Excluding the test-type switch is the fix to your problem as described here: https://code.google.com/p/chromedriver/issues/detail?id=1081#c6

When you do that then you get a warning:

Screenshot of warning Which you can remove by excluding the ignore-certificate-errors switch.

Hope it helps.

Upvotes: 0

LittlePanda
LittlePanda

Reputation: 2507

  1. Open Chrome
  2. Go to chrome://plugins/
  3. Check that the below plugin is enabled

enter image description here

To allow all plugins in Selenium WebDriver, try below Config:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("--always-authorize-plugins=true");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);

OR

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--load-extension=/path/to/extension/directory"));
WebDriver driver = new ChromeDriver(capabilities);

Upvotes: 2

Related Questions