Skip to content

Jsoup select elements with multiple classes example

Jsoup select elements with multiple classes example shows how to select elements having multiple classes separated by space characters.

How to select elements with multiple classes using Jsoup?

Many times HTML elements have multiple classes applied to them using the “class” attribute as given below.

It becomes a little tricky to select such elements using CSS style selectors. If the div element had a single class named “left-align”, you could have written Jsoup CSS selector like given below.

Output

If the element contains multiple classes, you can join them using separator “.” (dot) as given below.

Output

Please note that there is no space between the two classes.

Here is the full example program to select a div with multiple classes.

Output

Please also make sure that when you connect to a website using Jsoup, you set the user agent and referer header values.

This example is a part of the Jsoup tutorial with examples.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.