1

I am trying to return an array of elements of HTML that have the same class. I actually saw some similar questions such as

How to get the number of elements having same attribute in HTML in Watir?

Watir: How to retrieve all HTML elements that match an attribute? (class, id, title, etc)

HTML looks like this

<table class="module grid grid-50">
  <span bo-bind="row.date | sgDate">20/04/2018</span>
  <span bo-bind="row.date | sgDate">22/04/2018</span>
  <span bo-bind="row.date | sgDate">23/06/2018</span>
  <span bo-bind="row.date | sgDate">06/09/2018</span>
  <span bo-bind="row.date | sgDate">15/09/2018</span>
</table 

I have tried

content = browser.elements(:class => "module.grid.grid-50") puts content

which return <Watir::HTMLElementCollection:0x00000000053fc460>

Instead of this message, I would like to output an array of these dates. I think I am missing something here but do not know what exactly.

I read on Rubydoc that ElementCollection includes to_a method that returns collection as array and was wondering if it would be possible to include it here as well.

user12051965
  • 97
  • 9
  • 29
  • Can you expand on what you're trying to achieve or need? In general, you don't need to convert the HTMLElementCollection to an Array. HTMLElementCollection inherits all of the ElementCollection methods which also includes Enumerable. – Justin Ko Sep 13 '19 at 12:39

1 Answers1

0

The :class locator takes a list of classes rather than a CSS-selector. Looking for the class "module.grid.grid-50" tells Watir to look for an element like:

<div class="module.grid.grid-50"> 

If you want to match by multiple classes, you want to pass in an Array of individual classes:

tbl = browser.table(class: ['module', 'grid', 'grid-50'])
tbl.spans(bo_bind: /row.date/).map(&:text)
#=> ["20/04/2018", "22/04/2018", "23/06/2018", "06/09/2018", "15/09/2018"]
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • Thank you very much @JustinKo !! – user12051965 Sep 13 '19 at 15:18
  • @user12051965 Accept this answer if he answer your question. There is a hallow tic mark on the left side of the answer, if you click it , it will become green. The writer of the answer will get the credit of 15 points. – Rajagopalan Sep 15 '19 at 08:45