I have an html page with several of a certain type of element contained on a single page. How would you go about counting how many of these certain elements are present on the page with groovy? HTML looks something like this (with some extra divs between elements for styling purposes), would need to count how many ‘custom-list-items’ are present.
<div>
<div-list-holder>
<list>
<custom-list-item>
list item 1
</custom-list-item>
<custom-list-item>
list item 2
</custom-list-item>
<custom-list-item>
list item 3
</custom-list-item>
</list>
</div-list-holder>
</div>
Obviously the easy way to do this would be to count the length of the list/collection that holds all of the elements, but its a bit tricky for me with the nested elements. Looking for some type of WebUI.countOfElementsPresent(String elementName). I really don’t even need to know exactly how many there are, I just need to verify there are more than 1 of this specific element on the present page.
2
Answers
Use
Jsoup
for counting number oftags/elements
fromhtml
content as below-Example using groovy alone.