HtmlUnit asNormalizedText() returns empty string

Viewed 21

I have this code:

HtmlPage rowPage = ...
String address1 = ((HtmlDivision)rowPage.getFirstByXPath("//div[contains(@class, 'client_address1')]")).asXml();
System.out.println("address1 = " + address1);
String address1_2 = ((HtmlDivision)rowPage.getFirstByXPath("//div[contains(@class, 'client_address1')]")).asNormalizedText();
System.out.println("address1_2 = " + address1_2);

and my output is:

address1 = <div class="client_address1 clientRow">
  123 Somewhere ln
</div>

address1_2 = 

I expect asNormalizedText() to return 123 Somewhere ln. What circumstances would cause asNormalizedText to return nothing?

2 Answers

A bit more specific XPath would help

//div[contains(@class, 'client_address1')]/text()

What circumstances would cause asNormalizedText to return nothing?

From the javadoc:

Returns a normalized textual representation of this element that represents what would be visible to the user if this page was shown in a web browser.

Please check the css class - maybe the style hides the text.

Related