Reputation: 10777
I am trying to get some elements by their class names. Here is the HTML file:
<div id="container">
<div id="page_clips">
<div id="content" class="margin-right-5">
<div class="product-grid margin-left-5">
<div>.....</div>
<div>.....</div>
<div>.....</div>
</div>
</div>
</div>
</div>
Here, what i want to get is, the div that has the class="product-grid margin-left-5". Here is what i do:
Document doc = Jsoup.connect(link).get();
Element page_clips = doc.getElementById("page_clips");
Element page_clip_content = page_clips.getElementById("content");
This piece of code succesfully gets the div with the id "content". Then when i try,
Elements elementsIWantToGet= page_clip_content.getElementsByClass("product-grid margin-left-5");
it returns empty. What am i doing wrong? Isn't the name of the class attribute of that div is "product-grid margin-left-5" ? Can anyone help?
Thanks
Upvotes: 2
Views: 1884
Reputation: 1074575
Because getElementsByClass
doesn't support multiple class names. By giving it "product-grid margin-left-5"
, do you mean to find all elements with both classes? Or either class?
Either way, use select
, which accepts CSS selectors:
For elements with both:
.. = page_clip_content.select(".product-grid.margin-left-5");
For elements with either:
.. = page_clip_content.select(".product-grid, .margin-left-5");
Upvotes: 4