33

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.

ef2011
  • 10,431
  • 12
  • 49
  • 67

2 Answers2

54

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks + 1 for confirming that this indeed works. I have been using the latest Jsoup (1.5.2) but this still doesn't work for me. Could this be because my `
    ` has an id in addition to class? You are correct in assuming that the actual HTML I am trying to parse is slightly different, but AFAICT, the difference doesn't explain what I am experiencing. Should I post the full HTML section?
    – ef2011 May 27 '11 at 14:17
  • 2
    I would strip as much as possible from the HTML step by step as long as your function doesn't work. Once it works, go a step back and include it in your question, along with an elaboration where the conflicting part is. – BalusC May 27 '11 at 14:20
  • 1
    Thanks + 1 again. It turns out that I had one of those moments of blindness and I was logging something different from what I was debugging. Ugrrrr... – ef2011 May 27 '11 at 15:27
1

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

Jsoup selectors, jQuery selectors

Eliasz Kubala
  • 3,836
  • 1
  • 23
  • 28