2017-03-03 126 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

相關問題