Parthan P Vasu
Parthan P Vasu

Reputation: 63

selenium get the <div> count inside another <div>

I have to get the count of number of div inside another div My HTML code is like

<div id="list">
<div> some_data </div>
<div> some_data </div>
<div> some_data </div>
<div> some_data </div>
<div> some_data </div>
</div>

I tried with the code:

System.out.println(driver.findElements(By.xpath("//*[@id='list']")).size());

But getting result: 1 instead of 5

Is their any other way

Upvotes: 2

Views: 5719

Answers (2)

cruisepandey
cruisepandey

Reputation: 29362

Try this code :

List<WebElement> elements = driver.findElements(By.xpath("//div[@id='list']/div"));  

Now you have a list of divs, This list is nothing but an interface of collection interface.
Now you can perform multiple operations on it.Like

elements.get(int index);  
elements.clear();
elements.isEmpty();
elements.size(); 

and many more.

Hope this will help

Upvotes: 1

Murthi
Murthi

Reputation: 5347

try with the x-path //*[@id='list']/div as given below

System.out.println(driver.findElements(By.xpath("//*[@id='list']/div")).size());

Upvotes: 1

Related Questions