我使用的代码总是下载PDF.从最近开始,它开始在浏览器中打开PDF. chrome和firefox也是如此.
在chrome中我已经尝试过了:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-extensions"));
driver = new ChromeDriver(capabilities);
在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']
},
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)
这映射到首选项字符串,如“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)