Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jsoup select div having multiple classes

Tags:

java

jsoup

I am trying to select, using Jsoup, a <div> that has multiple classes:

<div class="content-text right-align bold-font">...</div>

The syntax for doing so, to the best of my understanding, should be:

document.select("div.content-text.right-align.bold-font");

However, for some reason, this doesn't work for me.

When I try the same exact syntax on JSFIDDLE, it works without a hitch.

Does multi-class selection work in Jsoup?

(I'd rather find out that this is a bug in my code than find out that this is a Jsoup limitation :)

UPDATE (thanks to the answer below): Jsoup works perfectly with the aforementioned syntax.

like image 812
ef2011 Avatar asked May 27 '11 13:05

ef2011


2 Answers

Works for me with latest Jsoup (1.5.2).

String html = "<div class=\"content-text right-align bold-font\">foo</div>";
Document document = Jsoup.parse(html);
Elements elements = document.select("div.content-text.right-align.bold-font");
System.out.println(elements.text()); // foo

So either you're possibly using an outdated version of Jsoup which exposes a bug related to this, or the actual HTML doesn't contain a <div> like that.

like image 71
BalusC Avatar answered Nov 07 '22 03:11

BalusC


It would by helpfull for you in near future. Have fun.

Jsoup selectors, jQuery selectors

like image 38
Eliasz Kubala Avatar answered Nov 07 '22 04:11

Eliasz Kubala