Search code examples
xpathhtmlunit

<td> value is not returned using HtmlUnit


When i tried using the below code...the td tag is returned and not the value of the td tag.

List<?> byXPath = page2.getByXPath("//tr[@class='metadata odd']/td");
System.out.println(byXPath.get(0).toString());

For ex:If the tag is

<td class='metadata odd'>Arun</td>

The result was

<td class='metadata odd'>

.... I need the result to be Arun. Kindly help


Solution

  • What you're looking for is actually the string representation of the text child node of the td tag, not the string representation of the td element itself, which you are requesting at the moment. Use a slightly different XPath expression to reference the text child node directly, like this:

    List<?> byXPath = page2.getByXPath("//tr[@class='metadata odd']/td/text()");
    DomText textNode = (DomText)(byXPath.get(0));
    System.out.println(textNode.toString());
    

    See also this question.