Search code examples
javascriptjavahtmlparsingjsoup

How to receive data attribute with jsoup?


i need to get data attribute from html I am trying to get like this

Elements element = document.select("div.highlight padding standard-box");
result+= element.attr("data-highlight-embed");

But result is empty, should be data-highlight-embed = content

html-code

<div class="highlight padding standard-box" data-link-tracking-page="Matchpage" 
data-link-tracking-column="[Main content]" data-link-tracking-destination="Click on highlight [button]" 
data-highlight-embed="content">text</div>

Solution

  • You need to change your CSS query and notice that the select() method return multiple elements.

    Update the CSS query to

    Elements element = document.select("div.highlight.padding.standard-box");
    

    Then you can loop the result

    for(Element el : element) {
        System.out.println(el.attr("data-highlight-embed"));
    }
    

    Or you can get the first element

    System.out.println(element.first().attr("data-highlight-embed"));
    

    To get the data attributes you also can reference how to use dataset() method at https://simplesolution.dev/java-jsoup-extract-custom-data-attributes-html5-element/