Count the number of elements with the same classname

I want to get the number of elements that have the same class name.


I tried def sz1 = driver.findElements(By.className(‘selection-item ng-star-inserted’)).size()

This returns sz1 as 0


String js = 'return document.querySelectorAll("").length;'
def sz1 = WebUI.executeJavaScript(js, null)
println sz1

By.className(string) expects a single class name as its sole argument. But you have 2 names in the class="selection-item ng-start-inserted" attribute. Therefore it does not work as you expected.

Try this.

def sz1 = driver.findElements(By.CssSelector(‘’)).size()

This would work the same as Russ’s proposal.

Thanks a lot @Russ_Thomas @kazurayam Both cases gave the result