Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select this element in JSOUP?

Tags:

java

jsoup

This is the HTML structure:

enter image description here

Element link = doc.select("div.subtabs p").first(); 

That does not seem to work. How do I select that p?

like image 661
HackToHell Avatar asked May 22 '12 04:05

HackToHell


People also ask

How do you select a specific element?

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

What does the CSS selector a href $= org select?

It matches links with href attributes whose values start with the given string.


2 Answers

The DIV with the class="subtabs" is not in fact the parent of the p element but instead is the sibling of p. To retrieve the p, you'll need to first get a reference to the parent DIV that has the id="content":

Element link = doc.select("div#content > p").first(); 

Additionally, you'll need the > symbol to indicate that you're selecting a child of div#content.

parent > child: child elements that descend directly from parent, e.g. div.content > p finds p elements; and body > * finds the direct children of the body tag

If you get stuck with a JSOUP CSS selector in the future, check out the JSOUP Selector Syntax cookbook, which has some nice examples and explanations.

like image 51
jmort253 Avatar answered Oct 04 '22 20:10

jmort253


div#content p. It is not a child of .subtabs.

like image 36
Hauke Ingmar Schmidt Avatar answered Oct 04 '22 19:10

Hauke Ingmar Schmidt