[英]find the particular value in the table column
i have write the below code for find the particular column value in web table, but if i give the static value in row and column value , driver is identifying the value , but if i get the value through for loop, i am not able to retrieve the values. 我已经编写了以下代码以在Web表中查找特定的列值,但是如果我在行和列值中给出静态值,驱动程序将识别该值,但是如果我通过for循环获取该值,则无法检索值。
WebElement tabledata = driver.findElement(By.id("divAttendanceDetails"));
List<WebElement> Rows = tabledata.findElements(By.xpath("//*[@id='divAttendanceDetails']/table[1]/tbody/tr"));
System.out.println("NoofRowsinthetable" + Rows.size());
String identifyvalue = "Leave Applied";
int leavecount = 0;
for (int getrowvalue=0; getrowvalue < Rows.size()-1;getrowvalue++)
{
List<WebElement> Columns = Rows.get(getrowvalue).findElements(By.xpath("//*[@id='divAttendanceDetails']/table[1]/tbody/tr/td"));
System.out.println("NoofColumnsinthetable" + Columns.size() );
for (int getcolumnvalue =0;getcolumnvalue<Columns.size(); getcolumnvalue++ )
{
String cellvaues = driver.findElement(By.xpath("//*[@id='divAttendanceDetails']/table[1]/tbody/tr["+getrowvalue+"]/td["+getcolumnvalue+"]")).getText();
System.out.println(cellvaues);
if(identifyvalue.equalsIgnoreCase(cellvaues))
{
leavecount = leavecount+1;
System.out.println("Leavecounttilldate" + leavecount );
}
}
}
Please help to resolve the issue 请协助解决问题
Html Page looks HTML Page的外观
<div id="newdiv"><table class="ariel" cellspacing="0" cellpadding="3" rules="all" border="1" id="dgResults" style="width:100%;border-collapse:collapse;">
<tbody><tr class="bluerow" align="left" style="font-weight:bold;">
<td style="width:15%;">start Date</td><td style="width:15%;">end Date</td><td style="width:15%;">in Time</td><td style="width:15%;">Out Time</td><td style="width:15%;">totalhours Office</td><td style="width:20%;">Details</td>
</tr><tr class="row2" align="left">
<td>01/01/2015</td><td>01/01/2015</td><td>00:00</td><td>00:00</td><td>00:00</td><td align="left">Holiday</td>
</tr><tr class="row2" align="left">
<td>01/02/2015</td><td>01/02/2015</td><td>00:00</td><td>00:00</td><td>00:00</td><td align="left">Leave Applied</td>
</tr><tr class="row2" align="left">
<td>01/03/2015</td><td>01/03/2015</td><td>00:00</td><td>00:00</td><td>00:00</td><td align="left">Weekend</td>
</tr><tr class="row2" align="left">
<td>01/04/2015</td><td>01/04/2015</td><td>00:00</td><td>00:00</td><td>00:00</td><td align="left">Weekend</td>
</tr><tr class="row2" align="left">
<td>01/05/2015</td><td>01/05/2015</td><td>13:02</td><td>19:01</td><td>04:38</td><td align="left"> </td>
</tr>
</tbody></table></div>
I have faced similar issue and have resolved by using String.format(). 我曾经遇到过类似的问题,并且已经通过使用String.format()解决了。
String xpath = "//*[@id='divAttendanceDetails']/table[1]/tbody/tr[%s]/td[%s]";
String cellvaues = driver.findElement(By.xpath(String.format(xpath, getrowvalue, getcolumnvalue))).getText();
one issue with yours xpath is its indexes. 您的xpath的一个问题是它的索引。 Xpath indexes starts with 1. Xpath索引以1开头。
ref: Why do indexes in XPath start with 1 and not 0? 参考: 为什么XPath中的索引以1而不是0开头?
but this can be issue if you have only one row, results for other rows should be displayed. 但这可能会出现问题,如果只有一行,则应显示其他行的结果。
It will be helpful if you share the HTML and your current output. 如果共享HTML和当前输出,这将很有帮助。
Followed the html
you have provided. 跟随您提供的html
。 It does not necessarily matches the code you have in original post. 它不一定与您原始帖子中的代码匹配。
List<WebElement> Rows = driver.findElements(By.cssSelector("#dgResults tr"));
System.out.println("NoofRowsinthetable" + Rows.size());
for (int i = 0; i < Rows.size(); i++)
{
//find the columns in specific row
List<WebElement> Columns = Rows.get(i).findElements(By.cssSelector("td"));
System.out.println("NoofColumnsinthetable" + Columns.size() );
for (int j = 0; j < Columns.size(); j++ )
{
String text = Columns.get(j).getText();
System.out.println(text);
/* adjust as you needed.
if(identifyvalue.equalsIgnoreCase(text))
{
leavecount = leavecount+1;
System.out.println("Leavecounttilldate" + leavecount );
}
*/
}
}
Note: I didn't test the code on my end. 注意:我没有在末端测试代码。 May have some syntax issue 可能有语法问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.