CSS selectors in Java


Here are some examples of CSS selector and equivalent XPath expressions:

CSS:   document > library > books > book
XPath: //document/library/books/book

CSS:   document book
XPath: //document//book

CSS:   document book#id3



This becomes more interesting when implementing pseudo-selectors in XPath:

CSS:   book:first-child

CSS:   book:empty






7 thoughts on “CSS selectors in Java

    1. Hi Rakesh,

      Version 1.0.0 already partially supports CSS selectors in $(..).find(), e.g.

      $(document).find(“books authors > author[name=Lukas]”);


  1. Lukas, it seems that we are bumping into one another in the blogosphere! I started to look at alternative to XPath for testing in order to implement functionality in ActiveWeb that is similar to Ruby on Rails assert_select: assert_select.

    I looked at the css-selectors by https://github.com/chrsan/css-selectors by Christer Sandberg and it seems like a perfect fit to test the views in ActiveWeb. However, I was unable to find it in Maven repository. Could you please share where you sourced it from?

    thank you,

    1. Igor, yes, it’s a small world! :-)

      Me too, I was thrilled to find a very comprehensive and standards-compliant CSS selector implementation in Java. I asked Christer if it was OK for me to include the relevant parts directly in jOOX, as I only needed the lexer / parser, not the execution facilities. Christer had no time so far to create a Maven Central artefact, but you could upvote adding css-selectors to Maven here:

      Or, optionally, create a dependency on joox, if you only need the scanner, too. It is available here:


Leave a Reply


WordPress.com Logo


Google photo


Twitter picture


Facebook photo


Connecting to %s
