Ross
Ross

Reputation: 97

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>

Upvotes: 1

Views: 972

Answers (1)

Tri
Tri

Reputation: 69

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/

Upvotes: 2

Related Questions