How do I select this element in JSOUP?

The p tag you are trying to extract is not a child of the div. It is a sibling. The parent div's id is content and the p tag you want is the first p tag within its parent. So use doc.select("div#content > p").first();

The # means id and > means RHS is a child to LHS. So the statement means get first paragraph which is child to div with id as content


The Chrome SelectorGadget is very helpful in constructing CSS selectors for jSoup, simply by point and click. It has saved me hours of development time when trying to target specific fields.


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.


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

Tags:

Java

Jsoup