Are you ready to use CSS selectors with selenium?

Nowadays CSS selectors become popular and took place of xpath selectors. While locating elements the ideal candidate is to reference elements on the page by their unique id or name. If that is not possible, incase not available or auto generated, then XPath or CSS are the strategies available to you. Here you can get answer on Why CSS Locators are the way to go vs XPath.

One useful tool I found is FirePath which is a Firebug extension that adds a development tool to edit, inspect and generate XPath 1.0 expressions, CSS 3 selectors and JQuery selectors (Sizzle selector engine). The problem with fire path generates xpath specific to element but for css or Sizzle it is not always specific to selected element.

Selenium IDE 1.0.11
released on 30/May/2011 has inbuilt CSS locator builder! Selenium IDE will now create locators using CSS when recording. Also, same as getXpathCount, new command getCssCount added to count the number of nodes that match the specified css selector. So now using css selector you can get number of nodes that match the specified selector.

Sizzle, used by selenium as CSS selector engine, provides Positional and Form Selector Additions makes locator simpler and more readable, for instance:

  • :nth(index_0_base)

    Simplified position selector (li:nth(5) finds the 6th li element)

  • :input

    Finds all input elements including textarea, select and button.

  • :text
  • :checkbox
  • :file
  • :password
  • :submit
  • :image
  • :reset
  • :button


    that finds the input element with the specified input type.
    Here :button also finds button elements in addition to input elements having type button.

You can find more details for additional selectors in Sizzle documentation.

I had documented some examples for defining complex CSS and X-Path selector.


X Path






locate first div element

By id



Locate div with id eleid

By class




locate div with class name eleclass if more than one class exist then xpath 2 will be used

By attribute

//div[@title='Move mouse here']

  1. div[title=Move mouse here]
  2. div[title^=Move]
  3. div[title$=here]
  4. div[title*=mouse]

you can use match operators for class and id as well, for example div[id^=menu-item]




div#eleid >*

div#eleid >h1




div h1 

Will work for //div/*/…../h1

By index



6th li element

By content

//a[contains(.,'Issue 1164')]

a:contains(Issue 1164)

Case Sensitive

By Child

  1. //li[a[contains(.,'Issue 1244')]]
  2. //*[./a[contains(.,'Issue 1244')]]
  3. //ul[.//a[contains(.,'Issue 1244')]]
  1. li{a:contains(Issue 1244)}
  2. ul{a:contains(Issue 1244)}

<ul><li> Improved alert on changing the format <a href=”">Issue 1244</a></li>

<li>next sibling</li>…</ul>

<h3>Listing 2</h3>

<ul><li> Improved alert on changing the format <a href=”">Issue 1244</a></li>

<li>next sibling</li>…</ul> 

Next Sibling 

  1. //li[preceding-sibling::li[contains(.,'Issue 1244')]]
  2. //ul[preceding-sibling::ul[.//a[contains(.,'Issue 1244')]]]
  1. css=li:contains(Issue 1244) + li
  2. css=ul{a:contains(Issue 1244)} ~ ul


If you have not read Why CSS Locators are the way to go vs XPath blog posts, you must absolutely read it now.

Limitations of CSS locators

CSS is used for styling the document/elements and not originally for locating elements, so there are certain limitations too.

  • There are some cases where you cannot locate element using CSS locator. For instance


  • Lack of arithmetic and logical operator/function support as compare to xpath
  • Text comparison is case sensitive and there is no way to perform case insensitive comparison.
  • When referring to parent you must be careful, For instance

In Xpath :

  • //*[./a[contains(.,'Issue 1244')]]
  • //li[./a[contains(.,'Issue 1244')]]

Both 1 and 2 are will locate element li, having first child a[contains(.,'Issue 1244')]

Whereas in CSS

  • li{a:contains(Issue 1244)}
  • *{a:contains(Issue 1244)}

Both 3 and 4 locates different elements. 3 is same as 1(assuming there is no other parent li element of li) but 4 locates first element that having child a:contains(Issue 1244) that is document root. There is no way to locate parent precisely.

  • For IE, Complex CSS might become as slow as XPath.


  • Element Location strategy should be selected with following preference

    id, name, link, dom/css/xpath

  • Locators recorded by IDE are not always efficient but you can modify it manually for the best efficient one.
  • Avoid locating parent using CSS or be more careful.


VN:F [1.9.10_1130]
Rating: 9.2/10 (21 votes cast)
Are you ready to use CSS selectors with selenium?, 9.2 out of 10 based on 21 ratings


{ 1 trackback }

CSS selectors and selenium (webdriver) | Walking in my shoes
December 18, 2012 at 7:38 pm

{ 3 comments… read them below or add one }

Ramesh November 25, 2011 at 1:02 am

a:contains(Issue 1164) is not working.
i tried
a:contains(“Issue 1164″) also.
Please tell me how to identify element with innertext.

Chirag Jayswal November 25, 2011 at 5:03 am

Hi Ramesh,
Will you please provide your code or some more details?
For CSS locator you need to provide explicit locator prefix, for instance, css=a:contains(Issue 1164)
In code you can use as:"css=a:contains(Issue 1164)");


Ramesh November 29, 2011 at 3:07 pm

<div class="ui-resizable-handle ui-resizable-ne" style="z-index: 1003;


i have to identify Continue button using contains.Like i have so many buttons with same class, i have only one way that i have to identify element with contains

Leave a Comment