Reputation: 55
I am using JSoup to parse some HTMLL information, and I would like to parse the aria label value of a specific div attribute. The line I am trying to parse is the following:
<div class="tiny-star star-rating-non-editable-container" aria-label=" Rated 5 stars out of five stars ">
I have used the following:
Document document = Jsoup.connect(url).get();
Elements stars= document.select("div.tiny-star star-rating-non-editable-container[aria-label]");
String value = stars.text();
System.out.println("The rating is " + value);
However, the String value, returns blank. Why is this?
Upvotes: 1
Views: 1732
Reputation: 122394
That selector expression won't give you what you expect. It's treated as a two-part selector
div.tiny-star
- find a div
element with class tiny-star
star-rating-non-editable-container[aria-label]
- then look for a descendant star-rating-non-editable-container
element which has an aria-label
attributeTry something more like
Element divWithStars = document.select(
"div.tiny-star.star-rating-non-editable-container[aria-label]");
String ariaLabel = divWithStars.attr("aria-label");
Note the dot rather than space between tiny-star
and star-rating-...
, and also the fact that select
returns the element that hosts the aria-label attribute, not the attribute itself - you have to use attr
to extract the attribute value.
Upvotes: 1