0

I am using getFirstByXPath on a HtmlTableRow with HtmlUnit, but it does not work properly.

If i log the HtmlTableRow asXml()

<tr class="odd">
  <td class="picColumn">
    <img src="xyz.jpg"/>
  </td>
  <td>

            Half Day Tour to the Rhine - Halbtages-Ausflug zum Rhein

  </td>
  <td>
    <b>
      Frankfurt am Main
    </b>
    <br/>

            ETS Gmbh &amp; Co. KG

  </td>
  <td class="dateColumn">

            01.04.2016
    <br/>

            15:15 Uhr

  </td>
  <td>
      Ticket
    </a>
  </td>
</tr>

Then I am trying to access different values with row.getFirstByXPath(xpath); which is only in following case fine: row.getFirstByXPath("//td[@class='dateColumn']");. If I try to access the second column with row.getFirstByXPath("//td[2]"); my result is null. What am I doing wrong?

Thank you!

user3287019
  • 311
  • 5
  • 18
  • I'm not sure this would help, but in any case you should avoid using the `//` selector : it's a waste of resources and a pain to maintain. Have your tried using `/tr/td[2]` ? – Aaron Apr 01 '16 at 13:14
  • I tested with `/tr/td[2]` and `/tr/td[@class='dateColumn']` right now, none of them has a result. Now are both of them null :-( – user3287019 Apr 01 '16 at 13:24
  • I'm not familiar with HtmlUnit so my XPath advice may not be so good in your case. Maybe the HtmlTableRow isn't the whole XML document? – Aaron Apr 01 '16 at 13:30
  • Looks like HtmlUnit's XPath resolution is made from the current node rather than from an irrelevant document root. Maybe you can try `./tr/td[2]` ? I hope someone who knows the framework will show up, I don't really think I can help you there... – Aaron Apr 01 '16 at 13:35

1 Answers1

0

//td[2] should work on the XML you provided, so this is strange. Can you try (//td)[2]?

Kim Homann
  • 3,042
  • 1
  • 17
  • 20
  • omg `(//td)[2]` works! Thank you very much!!! Its really strange why the `//td[2]` does not work, but anyway now you saved my day :) – user3287019 Apr 01 '16 at 13:44