Multiclass element selection clarification [duplicate]
Assuming several multiclass divs as demonstrated in the following HTML:
<div class="class_one class_two class_three classfour classfive classsix">
<div class="class_one class_two class_three classfour classfive">
<div class="class_one class_two class_three classfour classsix">
Is there a single Jsoup select expression that wil开发者_StackOverflow社区l select all 3 of them?
To clarify, thinking that the "lowest common denominator" will select all 3, I tried the following:
div[class=class_one class_two class_three classfour]
But it selected none!
On the other hand, using the full multiselect syntax works, but it can only select one of the above, e.g.:
div[class=class_one class_two class_three classfour classfive classsix]
Is there a way to select all 3 of them, using a single Jsoup select statement?
This is not specific to Jsoup, but to CSS. The [attribute=name]
selector does an exact match. Even the ordering matters. You want to use the .classname
selector here instead. The following should work:
Elements divs = document.select("div.class_one.class_two.class_three.classfour");
// ...
Note that ordering of the classnames doesn't matter here. This selector selects all <div>
elements which has all of the given classnames present.
See also:
- Jsoup selector syntax
- Jsoup
Selector
API javadoc
精彩评论