I used Elements...">

How to select an element with an empty class using Jsoup

I want to select an element with class="" as

 <li class="" > </li> 

I used

 Elements topProductSecNav = topNavWrapper.select("li[class=]"); 

but I got java.lang.IllegalArgumentException: String must not be empty exception java.lang.IllegalArgumentException: String must not be empty .

+5
source share
2 answers

Use this: Elements topProductSecNav=topNavWrapper.select(li[class=\"\"]");
See a working example here .

+2
source

I would use the cge regex li[class~=^$] selector:

 String html= "<li class=\"\" > </li>" + "<li class= > </li>" + "<li class > </li>" + "<li > </li>" + "<li class=\"test\" > </li>"; Document doc = Jsoup.parse(html,""); Elements liWithClassButNoName = doc.select("li[class~=^$]"); for (Element li:liWithClassButNoName){ System.out.println("li = "+ li); } 

leads to this result (only the first match is 3 lisa):

 li = <li class=""> </li> li = <li class> </li> li = <li class> </li> 

Explanation:

~= means regular expression and ^$ searches for an empty string

Jsoup will remove = in the second example, li element <li class= > . The regular expression also matches essentially with a nonexistent string, so if you need to filter them, you can go with the solution given by @TDG.

0
source

All Articles