Are you ready to use CSS selectors with selenium?

Reading Time: 7  min

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, in case 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.

Strategy X Path
Sizzle(CSS) Comments
Element //div div locate first div element
By id //div[@id=’eleid’] div#eleid Locate div with id eleid
By class //div[@class=’eleclass’]

//div[contains(@class,’eleclass’)]

div.eleclass 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]
Child //div[@id=’eleid’]/*

//div/h1

div#eleid >*

div#eleid >h1

Descendant //div[@id=’eleid’]//h1 div h1 Will work for //div/*/…../h1
By index //li[6] li:nth(5) 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

    //*[@value>3]

  • 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.

Conclusions:

  • 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.

 

More Resources:

 

Stay Updated
Please enable JavaScript in your browser to complete this form.
LinkedIn
Share
Copy link
URL has been copied successfully!

Other stories you may enjoy...

Healthcare Apps and the Need for Security

One of the most exciting areas in tech right now promises to be “the most personal” ever. A key aspect of making wearable devices like the Apple Watch personal is through...

Developing an App for the 2020 General Election?

Here is a thought: With the UK General Election having just finished, could the next one in 2020 be the first to use a mobile app to allow people to vote? The polling...

Be honest. Describe the state of your test cases.

“There’s some dead wood in there.” “Hmmm…. Someone really needs to clean them up.” “A little outdated.” For those reading this in the northern hemisphere,...