簡體   English   中英

如何使用ruby watir獲取<img>標簽的src屬性

[英]how to get src attribute of <img> tag using ruby watir

<table>
  <tr>
    <td>hello</td>
    <td><img src="xyz.png" width="100" height="100"></td>
  </tr>
</table>



tabledata.rows.each do |row|
  row.cells.each do |cell|
    puts cell.text          
  end
end
puts "end"      

得到輸出 - >

hello
end

我應該怎么做這樣的輸出 - >

hello
xyz.png
end

沒有使用Nokogiri。

獲取屬性

您可以使用Element#attribute_value方法獲取Element#attribute_value 例如,

element.attribute_value('attribute')

對於許多標准屬性,您還可以執行以下操作:

element.attribute

輸出單元格文本或圖像文本

假設單元格有文本或圖像:

  1. 您可以遍歷單元格
  2. 檢查圖像是否存在
  3. 輸出圖像src(如果存在)
  4. 否則輸出單元格文本

這看起來像:

tabledata.rows.each do |row|
  row.cells.each do |cell|
    if cell.image.exists?
      puts cell.image.src    #or cell.image.attribute_value('src')
    else
      puts cell.text
    end    
  end
end
puts "end" 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM