unknownbits
unknownbits

Reputation: 2885

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"      

getting output ->

hello
end

what should i do for output like this ->

hello
xyz.png
end

without using Nokogiri.

Upvotes: 5

Views: 2414

Answers (1)

Justin Ko
Justin Ko

Reputation: 46836

Getting an attribute

You can get the attribute of an element using the Element#attribute_value method. For example,

element.attribute_value('attribute')

For many standard attributes, you can also do:

element.attribute

Output cell text or image text

Assuming that a cell either has text or an image:

  1. You can iterate through the cells
  2. Check if an image exists
  3. Output the image src if it exists
  4. Else output the cell text

This would look like:

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" 

Upvotes: 9

Related Questions