保存PDF而不是在Selenium中打开 [英] Save PDF instead of opening in Selenium
问题描述
在chrome中,我已经试过了:
DesiredCapabilities功能= DesiredCapabilities.chrome();
capabilities.setCapability(chrome.switches,Arrays.asList( - disable-extensions));
驱动程序=新的ChromeDriver(功能);
在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屋!