【发布时间】:2014-06-28 01:04:51
【问题描述】:
我需要获取第一个类别标题和第二个类别标题之间的行数,以在 for 循环中用作增量。这是一个html示例:
<div class="datagrid-ready" range="0:49" chunk-id="0.0" style="width:100%;height:2220px;" enabled="true">
<div class="categoryHeader" template="group" row-id="0">...</div>
<div class="productTableRow" template="row" row-id="1">...</div>
<div class="productTableRow" template="row" row-id="2">...</div>
<div class="productTableRow" template="row" row-id="3">...</div>
<div class="productTableRow" template="row" row-id="4">...</div>
<div class="productTableRow" template="row" row-id="5">...</div>
<div class="categoryHeader" template="group" row-id="6">...</div>
<div class="productTableRow" template="row" row-id="7">...</div>
<div class="categoryHeader" template="group" row-id="8">...</div>
<div class="productTableRow" template="row" row-id="9">...</div>
<div class="productTableRow" template="row" row-id="10">...</div>
<div class="productTableRow" template="row" row-id="11">...</div>
<div class="categoryHeader" template="group" row-id="12">...</div>
<div class="productTableRow" template="row" row-id="13">...</div>
<div class="productTableRow" template="row" row-id="14">...</div>
</div>
这是我到目前为止整理的内容(显然它不起作用):
final int noItemsIn1stCat = driver.findElement(By.xpath("//div[@class='datagrid-ready']/div[contains(@class,'productTableRow')]")).size();
我不知道如何将5 变成noItemsIn1stCat。
使用 css 选择器会更容易或更可靠吗?
我是 java 和 selenium 的新手,所以任何帮助都将不胜感激!
【问题讨论】:
-
您的 div 没有结束标签。它们实际上是兄弟姐妹还是嵌套的?
-
你可以获得那些计算交点的元素。请参阅:stackoverflow.com/questions/24247331/… 和 stackoverflow.com/questions/24193807/…
-
如果可能的话,我建议您与开发人员交谈并要求他们以更智能和更可测试的方式重组 div,即将 .productTableRow 嵌套在 .categoryHeader 中。例如:
-
对不起,我从 Firebug 复制了 div 折叠以简化问题。 datagrid-ready div 是以下所有 div 的父 div。 (它们确实有折叠时不可见的结束标签。)
标签: java html xpath selenium-webdriver