1

行数的屏幕截图

如何在使用 Java 的 webdriver 中使用 xpath 获取 webtable 中的确切行数。使用下面的 xpath,我也可以使用标题进行计数。我不希望计数包括标题行并获取 webtable 的计数。

//table[@id='ctl00_MasterPlaceHolder_GrdHistory']/tbody/tr

HTML 代码:

<table id="ctl00_MasterPlaceHolder_GrdHistory" cellspacing="2" cellpadding="0" border="0" style="color:#333333;width:100%;">
<tbody>
<tr style="color:Black;background-color:#DFC065;font-weight:bold;">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;"   onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$0')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$1')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$2')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$3')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$4')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$5')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$6')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$7')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$8')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;"  onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$9')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$10')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$11')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$12')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color: rgb(251, 247, 234); text-decoration: none;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$13')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color:#FBF7EA;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$14')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">
<tr style="background-color:#FBF7EA;" onclick="javascript:__doPostBack('ctl00$MasterPlaceHolder$GrdHistory','Select$15')" onmouseout="javascript:setMouseOutColor(this);" onmouseover="javascript:setMouseOverColor(this);">

这可能吗,如果是这样,那么我如何在不包括标题行的情况下获得计数。请帮助我。帮助将不胜感激。

4

3 回答 3

6

使用下面的 xpath 排除第一行(标题)

//table[@id='ctl00_MasterPlaceHolder_GrdHistory']/tbody/tr[position()>1]
于 2013-02-20T10:38:51.780 回答
0

试试这个代码:

int count = (driver.findElements(By.xpath("//table[@id='ctl00_MasterPlaceHolder_GrdHistory']/tbody/tr")).size()) - 1;
于 2013-02-25T12:22:48.993 回答
0

我建议 :

 count(table[@id="ctl00_MasterPlaceHolder_GrdHistory"]/tbody/tr) - 1
于 2013-02-20T13:30:09.887 回答