views:

41

answers:

2

I have been banging my head on this for the better part of a day; I need to count the number of childNodes in a parent div. It basically is acting like a list and each childNode is a row I want to count. The html looks like:

div<@class="list "> 
   div<@id="list-item-01">
   div<@id="list-item-02">
   div<@id="list-item-03">
   div<@id="list-item-04">
   div<@id="list-item-05">
   ...
</div>

My primary approach has been to use the getEval() function in Selenium using some javascript.

examples that have failed:

String locator = "xpath=//div[contains(@class,'list')]";
String jscript = "var element = this.browserbot.findElement('"+locator+"');";
       jscript += "element.childNodes.length;";

String locator = "xpath=//div[@class='list']";
String jscript = "var element = this.browserbot.findElement('"+locator+"');";
       jscript += "element.childNodes.length;";

Now I know the element is there and my xpath is correct because I have tried using .isElementPresent and that returns true. So something is funky with Selenium and divs.

I also poked around with document.evaluate() as my javascript command but that proved equally fruitless.

A: 

So these divs are created dynamically . So when you create this , you will be using a variable for iteration like $i , $i++.

after printing divs add that value of $i to a hidden field.

if there are 3 divs , $i=3 , putvalue of hidden=3

just get the value of that field using javascript.

Or

Try these

http://api.jquery.com/parent/ http://api.jquery.com/children/

zod
+2  A: 

Why not use getXPathCount? Something like

getXPathCount("//div[contains(@class, 'list ')]/div[contains(@class, 'list-item-')]")

should do the trick.

.Net documentation

Java documentation

Wesley Wiser
since he wants the count of items within a div you may want to amend your xpath to take that into account
AutomatedTester
@AutomatedTester good point. I've updated my answer.
Wesley Wiser