Search code examples
javaurlweb-scrapinghtmlunit

HtmlUnit get element by class name containing string


I want to find any elements in the HtmlPage that have a class that contains the word 'date'.

ie i want to match any of the following:

<div class = 'date'> August 13 2017 </div>
<span class = 'pubDate'> August 12 2017 </div>
<div class = 'datePublished'> August 10 2017 </div>

In order to match exactly 'date' I am using the following:

HtmlPage page;
List<HtmlDivision> date = page.getByXPath("//div[@class='date']");
System.out.println(date.get(0));

Which is working correctly.

However, how do I change this (or what else should I use ) in order to be able to match any element that has a class name that contains the word date (case insensitive) ?


Solution

  • Try below XPath to match all div elements with attribute class that contains "date":

    //div[contains(@class, 'date')]