简体   繁体   English

在表格列中找到特定的值

[英]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.

相关问题 算法或SQL:查找确保结果集在特定列中具有值的一组列的条件总是&gt; 0 - Algorithm or SQL : to find where conditions for a set of columns which ensures result set has value in a particular column always > 0 TestNg Selenium-动态查找表列值 - TestNg Selenium- Find table column value dynamically 如何使用逗号从数据库表的特定列中拆分值? - How to split value using comma from the my database table's particular column? 如何隐藏 pivot 表中的特定列不在工作表中? - How to hide particular column in pivot table not in sheet? 从html表中的特定列获取值 - Getting values from a particular column in an html table 如何在级联上更改特定列值 - how to change particular column value on cascade 过滤与Java中特定列值相对应的行 - Filter rows corresponding to particular column value in java 通过迭代表和该特定值前面的 select 复选框来比较表中的值 - Compare value in table by iterating table and select checkbox in front of that particular value 如何从表中找到最大值和最小值以及另一列值? - How to find the maximum and minimum value along with another column value from a table? 如何在HBase表中查找在特定列中具有特定值的所有行 - How to find all rows in HBase table that have a specific value in a specific column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM