You may not have heard about this, but a while ago, Jason Huggins moved Selenium 1′s CSS locator engine from CSSQuery to Sizzle, jQuery’s CSS selector library.
While this may not sound like a big deal to most users, it actually is, and in this blog post, I’ll explain why and how to start using all the cool features that come with this change.

Why is Sizzle awesome?
Well, Sizzle is jQuery‘s selector engine, and that means A LOT. For those of you who don’t know, jQuery is the javascript library used by almost 30 percent of all the websites. (As in, “30 percent of the whole effing Internet!”).
Because of that, Sizzle gets an insane amount of usage, and therefore, testing. Sizzle’s code is used by an average of 1 site for every 3 you visit. Its codebase has over a thousand followers on github and more than 80 forks as of today. That’s a lot of eyes to catch bugs and improve performance.
But it’s not just a more robust and faster implementation of CSS that works on every browser. It has removed useless CSS selectors and added extra goodies that turn out to be pretty useful for people writing tests.
Sizzle’s extra features you can use right now
As it says in its docs, Sizzle not only implements virtually all CSS 3 Selectors, but also extends them a little bit and adds its own, including some that are actually pretty useful for writing Selenium tests:
:not
The :not selector will help you filter out elements that are similar, but not exactly what you’re looking for. Let’s imagine the following situation:
<a href="#meh" class="confirmation_link hidden">Confirm</a> ... lots of html ... <a href="#bleh" class="confirmation_link">Confirm</a>
As you can see here, there are two links in the page you’re trying to test. Since they both have the same text, link=Confirm won’t work because they’re the same class and there is no id you could use to be more specific. In this kind of situation, the :not selector is our perfect weapon. It’s just as simple as writing the following locator:
selenium.click("css=a.confirmation_link:not(.hidden)")
Thanks to Sizzle, complex filters can go inside :not. Here are some other examples:
selenium.click("css=.confirmation_link:not(div)")
selenium.click("css=.submit_button:not(#clear_button)")
selenium.click("css=input[type=button]:not(p#not_this_input > input)")
:contains
Even though :contains was already present in cssQuery and the old Selenium, I thought it was worth mentioning. It can be used to filter elements depending on their inner text, so you can do something like:
selenium.click("css=div#myID > a:contains(Confirm Transaction)")
:eq/:nth
This selector finds all the occurrences and then just filters the nth in the list. If are using the confusing :nth-of-type or :nth-child filters, this may be a great replacement.
selenium.click("css=table a:contains(Change password):nth(5)")
:header
This selector will find you any header element. That is, h1, h2, h3, h4, h5 or h6. Pretty cool, huh? This way you can forget about which type of header your devs choose to use in the page. All you care to know is that it’s going to be a header.
Asserting a header that contains a specific text is the perfect situation for this:
assert selenium.is_element_present(":header:contains(Users Admin)")
Form helpers
Additionally, Sizzle includes some form element shortcuts to save you from having to find out whether the element is a textarea element or an input. Even better, it saves you from writing ugly locators like input[type=checkbox].
:input: Finds all input elements (includes textareas, selects, and buttons).:text,:checkbox,:file,:password,:submit,:image,:reset,:button: Finds the input element with the specified input type (:button also finds button elements).
I think those are all important and you can check out the Sizzle’s wiki for more info. We also released a CSS selectors quick reference if you’d like to have a cheat-sheet printed and close to your desk while you’re writing your tests ;)
Hope everyone is now writing jQuery Selenium selectors and found this post useful for saving some time and headaches. Of course, all of this is already available in our browsers in the cloud service, Sauce OnDemand, so go try it out for free!

Highly interesting. Is this already included in any released version?
This sounds great !
However, I have not found a new release of Selenium 1.
Is that code available somewhere ?
Can we help to make new releases of Selenium IDE and Selenium RC that include that really cool change ?
Is this already available in slenium new release?
Thanks for article.
Please advice how convert “(” “)” characters when they exist in inner text.
Example: Link (here)
How correctly handle inner text using a:contains(“^Link …$”)
Do note that this is indeed Selenium 1, webdriver ?(aka Selenium 2) OTOH only falls back to Sizzle if native querySelector API is not available (which also means that you might be able to use Sizzle extensions on one browser and not on the other), which causes me ne end of headaches until I realized this…
And replying to myself, one can work around this problem by using driver.execute_script, see http://friendpaste.com/2syl0KjNXs3pPK4JTCZFAQ
Happy to say the answer is yes, this is included in all 2.0 versions of the Selenium jar (2.0a7 – 2.0b3). Notice this is available for Selenium 1 tests using the 2.0 jar (hope that’s not too confusing :)).
Same answer, this new locating engine will be available in all 2.0 versions of Selenium RC and IDE (from 2.0 alpha 7 to 2.0 beta 3).
Ditto, already avaiable in Selenium’s new jar. Get it from http://code.google.com/p/selenium/downloads/list (server standalone is what you’re looking for).
Sorry, can’t follow the link, but the conversion is simple, whitespaces are allowed:
//a[contains(text(), “hi I have whitespaces”)
Converts to
a:contains(hi I have whitespaces)
Thanks for sharing! Indeed, that’s correct. Selenium 2 will try to find a native CSS engine in the browser and only fallback to Sizzle on browsers where that’s not available (generally IE)…
We’re still debating wether that’s a good approach or not, stay tunned!
Hi Santiago,
I’ve tried :contains in latest jar, but it is throwing an exception. Is fall back sizzle implemented yet?
If you’re using Selenium 2.0 from C# and would like to use Sizzle selectors, you can use http://nuget.org/List/Packages/SizSelCsZzz.