Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
December 10, 2016

CSS Selector in Selenium WebDriver





CSS Selector in Selenium Web Driver will describe the importance of the CSS Selector in Selenium automation.It is very common that when you want to automate any application the most important thing is to identify the element to interact with it. Particularly when you using Selenium as your automation tool, explicitly you have to identify the element on web page. There are so many ways to identify the elements in Selenium and those are called locators. One of the most used, popular and fastest locators is CSS Selector.

CSS Selector is a way of identifying the element on web page. In simple words, finding address of an element on the web page. You can use so many ways to identify the element using CSS Selector.

Using CSS is best practice when you compare to XPath. The advantages of CSS over XPath are:

  • CSS Selector will not change browser to browser as XPath will change.
  • CSS is native to browsers and XPath is not
  • XPath can traverse up the document from a child element to parents.
  • XPath engines are different in each browser, hence make them inconsistent
  • IE does not have a native xpath engine, therefore selenium injects its own xpath engine for compatibility of its API. Hence we lose the advantage of using native browser features that WebDriver inherently promotes.

However there are some situations where, you need to use XPath and that will depend on the application.

By using below methods we can identify the elements uniquely.

  1. TagName
  2. Id
  3. ClassName
  4. Attributes
  5. Contains
  6. Starts-with
  7. Ends With





TagName:

Using tag name we can identify the element. But it will locate so many elements on the web page as page may have same tags in many places. So, it will not locate the element uniquely. This is not a good practice to use the tag name individually to locate the element.

Example:

input

CSS using input tag

In the above image, the input tag highlighted/located 15 elements. So, tag name may not useful while identifying the element uniquely.

ID:

ID is an attribute to the element or tag to describe what kind of element that is. IDs are         unique. If you want to use ID as locator then need to use ‘#’ symbol in front of ID.

#IDValue

Example:

#optionsRadiosInline1

CSS using ID

In the above example, only one element highlighted using ID. # is mandatory to identify the element using ID.

Class Name:

Class is also an attribute to the element to apply the styles to that tag/element. Class names are not unique. So, if use these; it locate so many elements which are using the same class. If you want to locate an element using class name use any other attributes as extra parameters to identify the element uniquely.

If you want to use Class Name as locator then need to use ‘.’(i.e. dot) symbol in front of class name.

.className

Example:

.radio-inline

CSS using Classname

In the above example, 2 elements highlighted using className. . (dot) is mandatory to identify the element using class name




Attributes:

    As class Names are not unique, need to use the other attribute values along with class name to identify element uniquely.

Sometimes single attribute is enough to identify, but some times it will not suffice to identify the element then we will use multiple attributes to identify the element uniquely.

Single Attribute:

tagName[attribute = ‘attributeValue’]

Example:

input[placeholder=’First name’]

CSS using Single Attribute

Multiple Attributes:

tagName[attribute1=’attributeValue1’][attribute2=’attributeValue2’]

Example:

input[type=’text’][placeholder=’First name’]

CSS using multiple attributes

Contains:

We can use contains method to identify the element. Need to use ‘*’ (i.e. Star) symbol to identify the element.

tagName[attribute*=’partialAttributeValue’]

Example:

input[placeholder*=’First’]

CSS using contains

Starts With:

We can use startswith method to identify the element. Need to use ‘^’(i.e Cap) symbol to identify the element.

tagName[attribute^=’attributeStartingValue’]

Example:

input[name^=’first’]

CSS using startswith

Ends With:

We can use endswith method to identify the element. Need to use ‘$’(i.e Dollar) symbol to identify the element.

tagName[attribute$=’attributeStartingValue’]

Example:

input[name$=’rstName’]

CSS using endswith

Conclusion:

Here the conclusion is, need to identify the element uniquely on the web page to interact with exactly. The above are the only examples to demonstrate. Those CSS will vary depends on the application and how the HTML is written.

Please watch the youtube video for better understanding.




Share this post: on Twitter on Facebook

XPath in Selenium WebDriver Selenium Webdriver Methods

Related Posts

TAKING WEB ELEMENT SCREENSHOT IN SELENIUM

Selenium - Java

How to Capture WebElement Screenshot in Selenium Webdriver using selenium 4

avoid Switch to window

Selenium - Java

How to Avoid Switch To Window in Selenium WebDriver

webdrivermanager

Selenium - Java

No More Driver EXE files for Selenium Execution – Instead Use WebDriverManager

HANDLE TEXTBOXES IN SELENIUM

Selenium - Java

Handling Textboxes in Selenium

JAVASCRIPTEXECUTOR IN SELENIUM

Selenium - Java

JavaScriptExecutor in Selenium

ELEMENT NOT VISIBLE EXCEPTION

Selenium - C#, Selenium - Java

Selenium ElementNotVisibleException

NOSUCHWINDOW EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium NoSuchWindowException

NOSUCHFRAME EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium NoSuchFrameException

NOSUCHELEMENT EXCEPTION

Selenium - C#, Selenium - Java

Selenium NoSuchElementException

SELENIUM WEBDRIVER EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium WebDriver Exceptions

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025