0

Below is my HTML structure of page.

<tr>
<td class="checkCol">
<td align="center">
<td> 8 </td>
<td> Add </td>
<td>
<td> Route Translation </td>
<td title=""> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> Force Complete </td>
<td>
<td>
<td>
<td>
</tr>

I am using below code to retrieve the TD element values.

List<WebElement> numOfRows = sppOrder_table.findElements(By.tagName("tr"));
    if (numOfRows.size() == 1) {
    System.out.println("No Record");
    } else {
    // Excluding header row
    for (int i = 1; i <= numOfRows.size() - 1; i++) {
        List<WebElement> numOfColumns = ((WebElement) numOfRows.get(i)).findElements(By.tagName("td"));
        for (WebElement td : numOfColumns) {
        System.out.println("Column Value === "+td.getText());
        }
    }

My Table Xpath is correct. It is printing nothing using HTMLUNITDRIVE and working fine using Firefox. Please suggest the resolution for this issue.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56

1 Answers1

0

It works with latest version. Your case is missing the header tr element.

The below prints the expected result:

<table id='myid'>
<tr></tr>
<tr>
  <td class="checkCol">
  <td align="center">
  <td> 8 </td>
  <td> Add </td>
  <td>
  <td> Route Translation </td>
  <td title=""> </td>
  <td> </td>
  <td> </td>
  <td> </td>
  <td> </td>
  <td> Force Complete </td>
  <td>
  <td>
  <td>
  <td>
</tr>
</table>
    HtmlUnitDriver driver = new HtmlUnitDriver();
    driver.get(the_url);
    WebElement sppOrder_table = driver.findElement(By.id("myid"));
    List<WebElement> numOfRows = sppOrder_table.findElements(By.tagName("tr"));
    if (numOfRows.size() == 1) {
        System.out.println("No Record");
    } else {
        // Excluding header row
        for (int i = 1; i <= numOfRows.size() - 1; i++) {
            List<WebElement> numOfColumns = ((WebElement) numOfRows.get(i)).findElements(By.tagName("td"));
            for (WebElement td : numOfColumns) {
                System.out.println("Column Value === "+td.getText());
            }
        }
    }
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
  • Hi Ahmed, I have the code for driver instance and Table element creation. I have posted only important code lines. Everything is working fine on Firefox driver. When I execute it on HtmlUnitDriver, it printing BLANK. – Ganesh Shinde Nov 05 '15 at 10:01
  • I you test with my posted example, HtmlUnit will work. Please provide your complete case for others to reproduce the issue. – Ahmed Ashour Nov 05 '15 at 10:02