0

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.

   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

  <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>
Krishna
  • 153
  • 4
  • 18

3 Answers3

0

I have faced similar issue and have resolved by using 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();
jags
  • 176
  • 5
  • it is not retrieve the value from the xpath.let me know i want to do some other changes in the code – Krishna May 05 '15 at 15:25
  • Does it throw any exception? Could you check putting a break point to check the data? Could you try getting the xpath values manually for all the indexes you are trying to iterate over to eliminate any culprit. – jags May 05 '15 at 15:31
  • It is not throwing any exception and also it is not printing any values in the console. yes if i give the xpath value manually, it is printing the values – Krishna May 05 '15 at 15:41
  • Guys any help on this issue – Krishna May 05 '15 at 18:28
  • I have used this approach in our projects and is working absolutely fine. Not sure what is happening here. Could be an issue in the webdriver version you are using? Just a pointer. – jags May 07 '15 at 07:42
0

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

ref: Why do indexes in XPath start with 1 and not 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.

Community
  • 1
  • 1
Kavan
  • 569
  • 4
  • 21
  • i hope that is not a issue. when i pass the value for rows and columns , i am getting the values. when using the loop only i am not getting any values – Krishna May 05 '15 at 18:28
0

Followed the html you have provided. 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

Saifur
  • 16,081
  • 6
  • 49
  • 73
  • This condition is print only the text available in each column. When i compare the string against the value using if condition , the count is not displayed correctly. total no of Row + 1 as displayed a count – Krishna May 08 '15 at 10:27
  • I am lost. Can you please clarify more? – Saifur May 08 '15 at 11:44