首页 > 解决方案 > Ruby - 具有自定义文件名的 Watir 保存文件

问题描述

如果我有以下 HTML:

<a class="name" title="file_name" href="/somelink>NAMEOFFILE</a>
    <div class="profile">
    <img class="FFVAD" decoding="auto" style="" sizes="496px" src="https://websitename.com/054a89a69181e68399c756d746f3b996/followme.jpg">
    </div>

如何使用 Watir 保存下载并使用链接标题保存图像。所以文件followme.jpg,将被下载并保存为title_name.jpg

标签: rubywatir

解决方案


抱歉,我最后想出了这个。我将元素的标题存储到一个字符串中,然后在调用文件写入时使用该字符串。

 @image_src = @browser.div(:class => "profile").image(:class => "FFVAD").src
    @userimage = @browser.link(:class => "name").text
          @filename = "./folder/#{@userimage}.jpg"

    File.open(@filename, 'wb') do |f|
             f.write open(@image_src).read
           end

推荐阅读