Auto Download files in watir

∥☆過路亽.° 提交于 2019-12-05 05:05:00

问题


How to auto download the excel files from the browser in one click on the link, without going through the "save as" and other windows in watir. I am trying to keep it OS independent, so would not be interested in using win32ole gem.


回答1:


for this task I tweaking my profile preferences

my code looks like this:

chrome driver:

profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.default_directory'] = download_directory
profile['download.prompt_for_download'] = false
browser = Watir::Browser.new :chrome, :profile => profile

chrome driver 2:

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

caps = Selenium::WebDriver::Remote::Capabilities.chrome
caps['chromeOptions'] = {'prefs' => prefs}
browser = Watir::Browser.new :chrome, :desired_capabilities => caps

firefox:

profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.lastDir'] = download_directory
profile['browser.download.folderList'] = 2
profile['browser.download.dir'] = download_directory
profile['browser.download.manager.showWhenStarting'] = false
profile['browser.helperApps.alwaysAsk.force'] = false
profile['browser.helperApps.neverAsk.openFile'] = "text/csv,application/pdf"
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv,application/pdf"
profile['pdfjs.disabled'] = true
browser = Watir::Browser.new :firefox, :profile => profile

(firefox example for me working only for pdf files)

but selenium browsers download has many bugs

some problem in chrome or firefox webdriver (like this http://code.google.com/p/chromedriver/issues/detail?id=130) do not allow write good tests for file downloads

I wrote the following ruby script for download files

require ‘rubygems’
require “net/http”
require “uri”

def download(_url, _download_path = ”)
    url = URI.parse _url
    http_object = Net::HTTP.new(url.host, url.port)
    http_object.use_ssl = true if (url.scheme == ‘https’ || url.port == 443)

    http_object.start.request_get(url.path) do |response|
        start_time = Time.now
        response["Content-Disposition"] =~ /^.+?filename=”(.+?)”$/
        file_name = $1
        file = open(_download_path + file_name, ‘wb’)
        length = response['Content-Length'].to_i
        response.read_body do |fragment|
            file.write(fragment)
        end
        file.close
        file_size = File.size(_download_path + file_name)/1024.0/1024.0
        puts “-“*80
        puts “Download time – #{Time.now – start_time}”
        puts “Download speed – #{file_size/(Time.now – start_time)} MB/s”
        puts “-“*80
    end
end

download(‘http://storagemadeeasy.com/files/1cf064a30aba6d1b8fbc0fba8ac8be5b.jpg’)

I hope this code will be useful for those who need test file download (not browser file download dialog window)




回答2:


It appears to be unique to each browser. Alister Scott wrote this << try that.



来源:https://stackoverflow.com/questions/13277364/auto-download-files-in-watir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!