0

I have the following html code:

I saw that for Watir-webdriver the "Watir::Image.file_size" method is not currently supported. I found out that the "Watir-Classic/Image.rb" has the same method, and it seems that can be used.

# this method returns the filesize of the image, as an int
def file_size
  assert_exists
  @o.invoke("fileSize").to_i
end

I created a method that should retrieve the image size, but it seems I am not initializing the object correctly. Here is my code from the method:

img_src="/location/on_the_server/image"
chart_image = Watir::Image.new(:src, img_src)
puts chart_image.file_size

The problem is that I receive the following error:

"ArgumentError: invalid argument "/location/on_the_server/image""

I saw that for initialization the object requires (container,specifiers). I tried to change the initialization line to "chart_image = Watir::Image.new(img_src, :src)" but the error keeps appearing.

Could anyone tell me what am I doing wrong?

Is there another way to get the file size of an image from a website?

Thank you.

4

1 に答える 1

2

Watir::Image を直接初期化するべきではありません。代わりにimage()、ブラウザまたは要素オブジェクトのメソッドを使用する必要があります。

#Assuming that browser = Watir::Browser that is open
img_src="/location/on_the_server/image"
chart_image = browser.image(:src, img_src)
puts chart_image.file_size

更新 - イメージをダウンロードしてファイル サイズを決定します。

open-uri (または同様のもの) を使用して画像をダウンロードし、Ruby の File クラスを使用してサイズを決定できます。

require 'watir-webdriver'
require "open-uri"

#Specify where to save the image
save_file = 'C:\Users\my_user\Desktop\image.png'

#Get the src of the image you want. In this example getting the first image on Google.
browser = Watir::Browser.new
browser.goto('www.google.ca')
image_location = browser.image.src

#Save the file
File.open(save_file, 'wb') do |fo|
  fo.write open(image_location, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
end

#Output the size
puts File.size(save_file).size
于 2012-06-26T13:13:11.137 に答える