保存PDF而不是在Selenium中打开 [英] Save PDF instead of opening in Selenium

查看:265
本文介绍了保存PDF而不是在Selenium中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个我使用的代码将永远下载PDF。最近它开始在浏览器中打开PDF。 chrome和firefox也是一样。

在chrome中,我已经试过了:

  DesiredCapabilities功能= DesiredCapabilities.chrome(); 
capabilities.setCapability(chrome.switches,Arrays.asList( - disable-extensions));
驱动程序=新的C​​hromeDriver(功能);

在firefox中,我尝试过:

  FirefoxProfile firefoxProfile = new FirefoxProfile(); 
firefoxProfile.setPreference(browser.download.folderList,2);
firefoxProfile.setPreference(browser.download.manager.showWhenStarting,false);
firefoxProfile.setPreference(browser。download。manager。useWindow,true);
firefoxProfile.setPreference(plugin.disable_full_page_plugin_for_types,application / pdf);
firefoxProfile.setPreference(browser.download.dir,C:\\Documents and Settings\\\xxxx\\My Documents\\\\Downloads);
firefoxProfile.setPreference(browser.helperApps.neverAsk.saveToDisk,application / pdf; text / plain; text / csv);
firefoxProfile.setPreference(pdfjs.disabled,true);
firefoxProfile.setPreference(browser.helperApps.alwaysAsk.force,false);
firefoxProfile.setPreference(plugin.scan.plid.all,false);
firefoxProfile.setPreference(plugin.scan.Acrobat,99.0);

但是,两个浏览器都打开PDF而不是保存它。



有什么想法?

解决方案

我可以告诉你我们是如何在Ruby中做到这一点的,希望你可以翻译它来适应你的Java(?)代码。主要的事情是搞清楚哪些首选项设置。

  Capybara.register_driver:selenium_chrome_downloads do | app | 
prefs = {
plugins:{
plugins_disabled:['Chrome PDF Viewer']
},
下载:{
prompt_for_download:false,
default_directory:'desired / download / path'
}
}
Capybara :: Selenium :: Driver.new(app,browser :: chrome,prefs:prefs)
end
Capybara :: Session.new(:selenium_chrome_downloads)

plugins.plugins_disabled,download.prompt_for_download和download.default_directory

有帮助的文档:
https://sites.google.com/a/chromium.org/chromedriver/capabilities (主要是Java)
https://code.google.com/p/selenium/wiki/RubyBindings(对于Ruby)


There was a code I use that would always download the PDF. Since recently it started to open the PDF inside the browser. The same happens for both chrome and firefox.

In chrome I already tried:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-extensions"));
driver = new ChromeDriver(capabilities);

And in firefox I tried:

FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser. download. manager. useWindow",true);
firefoxProfile.setPreference("plugin.disable_full_page_plugin_for_types", "application/pdf");
firefoxProfile.setPreference("browser.download.dir","C:\\Documents and Settings\\xxxx\\My Documents\\Downloads");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf;text/plain;text/csv");
firefoxProfile.setPreference("pdfjs.disabled", true);
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force",false);
firefoxProfile.setPreference("plugin.scan.plid.all",false);
firefoxProfile.setPreference("plugin.scan.Acrobat","99.0");

But still, both browsers are opening the PDF instead of saving it.

Any ideas?

解决方案

I can show you how we did it in Ruby, and hopefully you can translate it to fit your Java (?) code. The main thing is figuring out which preference keys to set.

Capybara.register_driver :selenium_chrome_downloads do |app|
  prefs = {
    plugins: {
      plugins_disabled: ['Chrome PDF Viewer']
    },
    download: {
      prompt_for_download: false,
      default_directory: 'desired/download/path'
    }
  }
  Capybara::Selenium::Driver.new(app, browser: :chrome, prefs: prefs)
end
Capybara::Session.new(:selenium_chrome_downloads)

This maps to preference strings like "plugins.plugins_disabled", "download.prompt_for_download", and "download.default_directory"

Helpful docs: https://sites.google.com/a/chromium.org/chromedriver/capabilities (mostly Java) https://code.google.com/p/selenium/wiki/RubyBindings (for Ruby)

这篇关于保存PDF而不是在Selenium中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆