2

「名前を付けて保存」やwatirの他のウィンドウを経由せずに、リンクを1回クリックするだけでブラウザからExcelファイルを自動ダウンロードする方法。OSに依存しないようにしようとしているので、win32ole gemの使用には興味がありません。

4

2 に答える 2

6

このタスクのために、プロファイルの設定を微調整します

私のコードは次のようになります。

クロームドライバー:

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

クロム ドライバー 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

ファイアフォックス:

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の例は、pdfファイルに対してのみ機能します)

しかし、セレンブラウザのダウンロードには多くのバグがあります

Chrome または Firefox の Web ドライバーの問題 (このhttp://code.google.com/p/chromedriver/issues/detail?id=130など) により、ファイル ダウンロード用の適切なテストを作成できません。

ダウンロードファイル用の次のrubyスクリプトを作成しました

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’)

このコードが、テスト ファイルのダウンロードが必要な人に役立つことを願っています (ブラウザのファイル ダウンロード ダイアログ ウィンドウではありません)。

于 2014-02-24T14:05:30.160 に答える
2

各ブラウザに固有のようです。Alister Scott がこれを書きました<< あれを試してみてください。

于 2012-11-07T20:47:56.657 に答える