【问题标题】:chrome webdriver - PDF documents downloading and not opening in new tabchrome webdriver - PDF 文档正在下载,而不是在新选项卡中打开
【发布时间】:2016-07-19 15:21:26
【问题描述】:

我发现了一个问题,我将使用谷歌浏览器单击网页内的链接到 PDF 文档。以前,当我单击链接时,PDF 文档会加载到新选项卡中。然后我会围绕文档执行一些验证。

最近,测试开始失败,因为当我单击 PDF 文档的链接时,它会下载 PDF 文件,而不是在新选项卡中打开它。我假设该问题是由于 chrome 或 chrome 驱动程序的更新而最近发生的。

如果我手动检查链接,即不通过 chrome 驱动程序实例,那么它将在新选项卡中打开就好了。有没有人遇到过这个问题?

【问题讨论】:

  • 您是否启用或禁用了 Chrome PDF 查看器插件?
  • 我已启用插件 ok

标签: selenium-webdriver webdriver chrome-web-driver


【解决方案1】:
  1. 打开 Chrome
  2. 转到 chrome://plugins/
  3. 检查以下插件是否启用

要允许 Selenium WebDriver 中的所有插件,请尝试以下配置:

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

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

【讨论】:

  • 我的插件已经启用了 ok
  • 尝试使用ChromeOptions
【解决方案2】:

刚刚解决了一个类似的问题。这是我的 JS 代码:

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();

排除 test-type 开关可以解决您的问题,如下所述:https://code.google.com/p/chromedriver/issues/detail?id=1081#c6

当你这样做时,你会收到一个警告:

您可以通过排除 ignore-certificate-errors 开关来删除它。

希望对你有帮助。

【讨论】:

    【解决方案3】:

    当我们在配置文件首选项中启用 Chrome PDF 查看器插件时,这可以正常工作:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 2017-05-19
      • 2016-02-15
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      相关资源
      最近更新 更多