HtmlUnit: how to search for text

In a java project, I have to use HtmlUnit to retrieve the contents of a WebPage.

How can I search for an element that has the contents 123. It is part of an html document. The referring element has no id, class or name:


It is not possible to convert the whole document into a text and search afterwards.


  • You need to compose the appropriate xpath expression. In your case it can be:


    or more generally


    To find elements: page.getByXPath("//*[contains(text(),'123')]")