Count the number of elements with the same classname

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

image

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

This returns sz1 as 0

Thanks

String js = 'return document.querySelectorAll(".selection-item.ng-star-inserted").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(‘div.selection-item.ng-star-inserted’)).size()

This would work the same as Russ’s proposal.

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