2017-03-03 87 views
0

使用Chrome运行WebDriver(Ruby)时,具体的命令行开关desired_capabilities将禁用Chrome的内置PDF查看器。Selenium WebDriver(Ruby):如何下载PDF文件?

换句话说,如何以编程方式将Chrome配置为下载PDF文件而不是使用其内部PDF查看器插件打开?

+1

看看这个链接http://stackoverflow.com/questions/31871087/save-pd硒替代开启 – shank087

回答

0

基于所提供Save PDF instead of opening in Selenium后续的资源,我发现这个工作根据需要

prefs = { 
    plugins: { 
     plugins_disabled: ['Chrome PDF Viewer'] 
    }, 
    download: { 
     prompt_for_download: false, 
     directory_upgrade: true, 
     default_directory: 'desired/download/path' 
    } 
} 

browser = Selenium::WebDriver.for :chrome, prefs: prefs 

必须有没有目录路径斜杠

1

更新答案:铬61:红宝石2.3: ChromeDriver 2.32.498537

 download_prefs = { 
     prompt_for_download: false, 
     default_directory: 'desired/download/path' 
     } 

     plugin_prefs = { 
     always_open_pdf_externally: true 
     } 

     options = Selenium::WebDriver::Chrome::Options.new 
     options.add_preference(:download, download_prefs) 
     options.add_preference(:plugins, plugin_prefs) 
     driver = Selenium::WebDriver.for :chrome, options: options 
+0

为什么我会看到'未初始化的常量Selenium :: WebDriver :: Chrome :: Options',即使我有'require'硒-webdriver''? – Noffica

相关问题