javahtmlunit

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:

....
  <tr>
    <td>123</td>
    <td>456</td>
  </tr>
....

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


Solution

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

    //*[text()='123']
    

    or more generally

    //*[contains(text(),'123')]
    

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