【问题标题】:How to read values of "data-contents"-attributes from spans with watir?如何从具有 watir 的跨度中读取“数据内容”属性的值?
【发布时间】:2016-05-21 22:06:45
【问题描述】:

我需要从特定页面的所有 span 中读取名为“data-contents”的属性的值

<span tabindex="0" title="" class="text-warning  ng-isolate-scope" role="button" data-original-title="" data-toggle="popover" data-placement="top" data-html="true" data-contents="If you are not sure which type of occupation in the list to choose, please call us on (852) 2884 8888 and we'll be happy to help you." data-content="If you are not sure which type of occupation in the list to choose, please call us on (852) 2884 8888 and we'll be happy to help you." data-da-popover="" data-da-popover-id="daPopover1455178167018" data-container="body" data-trigger="focus"><img src="/app/ui/images/question-icon.png">          </span>

我已经写了以下代码,

  b.spans.each do |span|
        puts span.data-contents
  end

但它会抛出此错误':未定义的局部变量或方法`contents' for main:Object (NameError)'

谁能帮我读一下这个数据内容的价值?

【问题讨论】:

  • 可能与attribute-accessor 一样在stackoverflow.com/questions/1443455/… 中?
  • 是的,太好了,可以了,谢谢。
  • 然后让我们关闭为重复
  • 是的,谢谢你指导我。
  • 对于 html5 有效属性(如 'data-*'),Justin 下面的回答应该优于 attribute_value 方法。

标签: ruby watir watir-webdriver watir-classic


【解决方案1】:

您可以像获取其他属性一样获取数据以及 aria 属性。唯一的区别是方法名称使用下划线而不是破折号。

要获取 data-contents 属性,您可以:

puts span.data_contents

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    相关资源
    最近更新 更多