Count Number of elements in list

Hi all,

As seen the solution of oliver howard,

import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

WebDriver driver = DriverFactory.getWebDriver()
WebElement temp = driver.findElement(By.className(‘trip-cards’))
List list = temp.findElement(By.xpath(‘.//li’))

This solution is good to work, the problem which i am getting is that list is counting the className as repetitive times i.e suppose there is a class ‘image’ and on that particular page there are 300 classes of that so it is sometimes counting it as 350 or 374, etc.

So, i used sets too i couldn’t find a solution, please help me out.

Im not quite sure what you’re asking, but you basically want to tweak your xpath to only put elements into your list that you’re interested in I think.

The below would give you a list of all non invisible elements .

DriverFactory.getWebDriver().findElements(By.xpath("//[contains(@class, ‘trip-cards’) and not(ancestor::[contains(@style, ‘display: none’)])]/child::li")).size();

Yes, i am taking the xpath to put my elements into that list but the problem i am facing is that like i am having classname= imageView, and that classname is suppose having 300 times on that page, so when i put that xpath into the list its sometimes counting the xpath more than 300 times i.e taking one item as multiple times, so how can i solve it.

Is there any solution??..i am stucked

It won’t be taking one element multiple times. It’ll just be that class is used on lots of elements. Can you show us the DOM of the page? If we can see what you do want in your list and what you don’t want then that would make it easier

//Count Listing

//WebDriver driver = DriverFactory.getWebDriver()

//List list = driver.findElements(By.className(‘imageView’))


//def countList=list.size()

//KeywordLogger printlog=new KeywordLogger()

//printlog.logInfo(“( “+list.size().toString()+” PRODUCTS “+”)”)

So, this is the problem i am getting, i am counting the total class=‘imageView’ on this page, list is counting the elements but repeating the count of elements.

Screenshot (63).png

It seems fine for me. Not repeating anything. You could try using findElements by xpath instead.

List list = driver.findElements(By.xpath("//li[contains(@class, ‘imageView’)]"))
List list = driver.findElements(By.xpath("//ul[contains(@class, ‘prodBox’)]/child::li"))

Both of those returned 338 products on that page for me.

1 Like

Thanks Tom it worked.
I was taking the xpath wrong that’s why it was sometimes counting right, sometimes not.

Thank you very much.

Hi, Im new to Katalon and groovy. Previously I use selenium + python but with very basic knowledge. So now my question is, how can I do this thing twice? Meaning let say, on top I want to check list product and below maybe I want to check list of blog post for example.

I cant write the list.list twice because it’s already call at the first list right. In python and selenium I cant do def xxx(self) but with Katalon, I dont know how to do it. Hope you guys can help.