在表列中查找特定值

时间:2022-05-23 19:18:50

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页面看起来

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

3 个解决方案

#1


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();

#2


one issue with yours xpath is its indexes. Xpath indexes starts with 1.

与你的xpath的一个问题是它的索引。 Xpath索引从1开始。

ref: Why do indexes in XPath start with 1 and not 0?

ref:为什么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和当前输出将会很有帮助。

#3


Followed the html you have provided. It does not necessarily matches the code you have in original post.

按照你提供的HTML。它不一定与原始帖子中的代码相匹配。

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

注意:我没有测试我的代码。可能有一些语法问题

#1


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();

#2


one issue with yours xpath is its indexes. Xpath indexes starts with 1.

与你的xpath的一个问题是它的索引。 Xpath索引从1开始。

ref: Why do indexes in XPath start with 1 and not 0?

ref:为什么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和当前输出将会很有帮助。

#3


Followed the html you have provided. It does not necessarily matches the code you have in original post.

按照你提供的HTML。它不一定与原始帖子中的代码相匹配。

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

注意:我没有测试我的代码。可能有一些语法问题