Search code examples
rubywatirwatir-webdriverwatir-classic

How to read values of "data-contents"-attributes from spans with watir?


I need to read the value of the attribute named "data-contents" from all the spans in a particular page

<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>

I have written the following code,

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

But it throws this error ': undefined local variable or method `contents' for main:Object (NameError)'

Can anyone please help me to read the value of this data-content?


Solution

  • You can get data, as well as aria, attributes just like other attributes. The only difference is that the method name uses underscores instead of dashes.

    To get the data-contents attribute, you would do:

    puts span.data_contents