加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何使用ruby watir获取标记的src属性

发布时间:2020-12-17 01:25:33 所属栏目:百科 来源:网络整理
导读:table tr tdhello/td tdimg src="xyz.png" width="100" height="100"/td /tr/tabletabledata.rows.each do |row| row.cells.each do |cell| puts cell.text endendputs "end" 获得输出 – helloend 我应该怎么做这样的输出 – helloxyz.pngend 没有使用Nokog
<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

输出单元格文本或图像文本

假设单元格有文本或图像:

>您可以遍历单元格
>检查图像是否存在
>输出图像src(如果存在)
>否则输出单元格文本

这看起来像:

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"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读