English 中文(简体)
Selenium:如何查找div的子节点数
原标题:Selenium: how to find the number of childNodes of a div

一天的大部分时间里,我一直在思考这个问题;我需要计算父div中的子节点数。它基本上就像一个列表,每个子节点都是我想要计算的行。html看起来像:

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>

我的主要方法是使用一些javascript在Selenium中使用getEval()函数。

失败的示例:

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;";

现在我知道元素在那里,我的xpath是正确的,因为我尝试过使用.isElementPresent,结果返回true。所以硒和divs有点奇怪。

我还试着用document.eevaluate()作为我的javascript命令,但同样没有结果。

问题回答

为什么不使用getXPathCount?有点像

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

应该做到这一点。

.Net文档

Java 文件





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签