When working with HTML in Watir-Webdriver, determining how many elements of a certain attribute exist is done through the
.size function. Determining this element count has multiple uses. If you simply need to confirm that there is a certain number of a specific element, you can easily do so. If you are debugging, it may provide the first step into a series of solutions, as you can determine if your script is identifying a duplicate element in an incorrect location. Here are two scenarios to illustrate how the elements are checked (listing the HTML we are examining and the corresponding Watir command):
browser.links(:href,"http://3qilabs.com/about/").size # => 1
<li id="menu-item-23" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-23 parent-nav">
browser.links(:href,"http://3qilabs.com/about/").size # => 2
We are returned with a count of 1 for the initial example, as there is only 1 instance of a link through to "
http://3qilabs.com/about/". The second example, we have 2 instances of the link, thus the returned value of 2.
.size function can only be engaged by adding an "s": to the end of the element type, pluralizing it. The element type we are using above is ".link", which becomes ".links". The .
size function can be used in identifying any type of element within the HTML: