January 18, 2011

Locating by CSS

Locating by CSS:


CSS (Cascading Style Sheets) is a language for describing the rendering of HTML and XML documents. CSS uses Selectors for binding style properties to elements in the document. These Selectors can be used by Selenium as another locating strategy.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

 
   id="loginForm">
    class="required" name="username" type="text" />
    class="required passfield" name="password" type="password" />
    name="continue" type="submit" value="Login" />
    name="continue" type="button" value="Clear" />
  


  • css=form#loginForm (3)
  • css=input[name="username"] (4)
  • css=input.required[type="text"] (4)
  • css=input.passfield (5)
  • css=#loginForm input[type="button"] (4)
  • css=#loginForm input:nth-child(2) (5)
For more information about CSS Selectors, the best place to go is the W3C publication. You’ll find additional references there.

Note
Most experienced Selenium users recommend CSS as their locating strategy of choice as it’s considerably faster than XPath and can find the most complicated objects in an intrinsic HTML document.

No comments:

Post a Comment