2015-10-19 89 views
1

我想使用Watir Webdriver從頁面下載文件。我需要能夠將下載位置設置爲我定義的目錄。我發現this page,但那裏的代碼無法正常工作:使用Watir Webdriver下載文件

profile = Selenium::WebDriver::Chrome::Profile.new 
profile['download.prompt_for_download'] = false 
profile['download.default_directory'] = download_directory 

b = Watir::Browser.new :chrome, :profile => profile 

(其中download_directory只是我的桌面)給出了這樣的:

Selenium::WebDriver::Error::UnknownError: unknown error: cannot parse capability: chromeOptions 
from unknown error: unrecognized chrome option: profile 
    (Driver info: chromedriver=2.19.346063 (38b35413bd4a486d436a9749e090454bc9ff6708),platform=Mac OS X 10.11.0 x86_64) 
from /Users/myusername/.rvm/gems/ruby-1.9.3-p551/gems/selenium-webdriver-2.48.1/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok' 

我猜的代碼是爲老版本的的Watir。如何通過Watir設置Chrome的下載位置?

回答

4

你的榜樣工作了韋里老鉻(webdriver的1)則必須將此代碼:

prefs = { 
    'download' => { 
     'default_directory' => download_directory, 
     'prompt_for_download' => false, 
    }, 
    'profile' => { 
     'default_content_settings' => {'multiple-automatic-downloads' => 1}, #for chrome version olde ~42 
     'default_content_setting_values' => {'automatic_downloads' => 1}, #for chrome newe 46 
    } 
} 

caps = Selenium::WebDriver::Remote::Capabilities.chrome 
caps['chromeOptions'] = {:prefs => prefs} 

b = Watir::Browser.new :chrome, :desired_capabilities => caps 

更可以蘆葦在我的舊answer在這個問題上

2

大廈Stephan's answer,我發現我可以使用這個較短/清潔版本:

preferences = { 
    :download => { 
    :prompt_for_download => false, 
    :directory_upgrade => true, 
    :default_directory => "download_directory" 
    } 
} 

browser = Watir::Browser.new :chrome, :prefs => preferences