question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

".class" selector seems to be failing

See original GitHub issue

A more detailed post: http://stackoverflow.com/questions/42322374/jsoup-basic-class-css-selectors-not-working-html-shows-elements-exist-an

The problem can be summarized like so:

Debugging code:

Elements as = doc.select("a");
for (Element e : as) {
    if (e.classNames().size() > 0)
        M.debug(e.classNames());
    if(e.classNames().contains("bigChar")) {
        M.debug("found!");
    }
}
as = doc.select(".bigChar");
M.debug("now: " + as);
for (Element e : as) {
    if (e.classNames().size() > 0)
        M.debug(e.classNames());
}

Output:

[logo]
[bigChar]
found!
[dotUnder]
[dotUnder]
[dotUnder]
[dotUnder]
[dotUnder]
now: 

I’ve spent a while debugging this and I think the problem may have to do with malformed HTML. Attached is a sample input, and below is some simple code to run it (I am able to reproduce this problem consistently by reading from the file):

    try {
        Document doc = Jsoup.parse(new File("blahblah.txt"), "UTF-8");
        System.out.println(doc.html());
        System.out.println("FINAL");
        System.out.println(doc.select(".bigChar"));
        System.exit(0);
    } catch (IOException e) {
        e.printStackTrace();
    }

blahblah.txt

You can see in the HTML output that the “bigChar” class is used, but the .bigChar selector doesn’t seem to find it.

In my run of the above code, my output is

   </div> 
   <script type="text/javascript">
         $('#footer').css('top', $(document).height() - $('#containerRoot').height());
     </script>   
  </body>
 </html>
 FINAL
 

(i.e. nothing found for .bigChar)

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jhycommented, Jun 10, 2017

Actually this is the same as #814 as @cketti pointed out 😃 I just forgot that try.jsoup wasn’t on 1.10.3.

1reaction
jhycommented, Jun 10, 2017

Thanks for the report and the sleuthing.

The problem is that the attribute name is now all lower case, and something’s bugged in the case insensitivity of the attribute name.

You can see an e.g. here: http://try.jsoup.org/~l4ZA3QJ3p5WzWTD5y6v-tGu1Sh8 - change “Class” to “class” and it works. Will fix it up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jquery Class Selector Fails to Select new Class Instances ...
I'm semi-new to Javascript/jQuery so apologies in advanced if I'm missing something basic. I have a function that is triggered whenever a user...
Read more >
Problems with the New Class Selector - Forum | Webflow
Renaming a global class is destructive and will break in all cases as the warning in the style manager indicates.
Read more >
Class selectors - CSS: Cascading Style Sheets | MDN
The CSS class selector matches elements based on the contents of their class attribute.
Read more >
Step 52 . I added the dessert selector but seem to fail
Tell us what's happening: Describe your issue in detail here. **Your code so far** /* file: index.html */ <!DOCTYPE html> <html lang="en"> ...
Read more >
Little CSS Stuff Newcomers Get Confused About
The first one will select any element with that class name. ... It seems like as far as CSS is concerned, using classes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found