Selenium: how to find the number of childNodes of a div
I have been banging my head on this for the better part of a d开发者_运维知识库ay; 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.
Why not use getXPathCount
? Something like
getXPathCount("//div[contains(@class, 'list ')]/div[contains(@class, 'list-item-')]")
should do the trick.
.Net documentation
Java documentation
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/
精彩评论