How to Use CSS Selector in Selenium WebDriver | 13 Examples

Selenium Webdriver is very powerful to automate web applications. We can identify web elements in Selenium Webdriver using various locators and CSS Selector is one of them.CSS Selector in Selenium acts as the best workaround to find an element when we don’t have an option to locate an element using Id or Name. Also, it is faster and simpler than XPath.To know more about XPath please refer exclusive article on Xpath XPath in Selenium WebDriver with Example.In the following examples, we will see how to use CSS Selectors in Selenium.

CSS Selectors by Attribute

Suppose we have an Html tag with the following attributes.

The syntax to locate elements by attribute are as follows

tagName[attributename=’attributeValue’]

Example 1

Since name might not be a unique property on the web we can combine more than one attribute to find an element uniquely. For that, the syntax is as follows:

Example 2

CSS Selector Tag and ID Attribute

In CSS, a hash (#) notation refers to the id attribute of an element.

Syntax 1: <Html tag><#><ID attribute value>
Syntax 2: <#><ID attribute value>

Example 1:

Example 2:

CSS Selector Class Attribute

A dot (.) notation is used to refer to the class attribute of an element.

Syntax 1: <Html tag><.><ID attribute value>
Syntax 2: <.><Class attribute value>

Example 1:

Example 2:

Handling Multiple Classes or Space Character in Class Name

Suppose we have an Html tag with the following attributes.

In the above HTML tag, three classes (abc, pqr, and xyz) are being used for the single text. box. You can not write a statement like By.cssSelector(“.abc pqr xyz”) to identify an element.If you write such code Selenium will throw NoSuchElementException error. Developers can use such class having multiple class names for different elements on the page. Out of all the class names, a few of the names might be unique. To handle such a scenario we have to join the classes using a dot(.) notation unless the required element is identified uniquely. The following are the possible ways to refer to the element.

  • css=.abc
  • css=.abc.pqr
  • css=.pqr.xyz
  • css=.abc.xyz
  • css=.abc.pqr.xyz

Example:

Locate Elements with Sub-strings or Partial Match

Using CSS locators, we can also locate elements with some matching patterns or sub-strings. It becomes very helpful to identify elements with dynamically generated ids. This is achieved by using three special characters ^, $, and *.

‘^’ symbol, represents the starting text in a string.
‘$’ symbol represents the ending text in a string.
‘*’ symbol represents contains text in a string.

Let’s consider a web page has the following HTML Tags

CSS Selector Starts With

It helps in locating element that starts with a particular value. we would use ^= which means “starts with”.

Syntax:
css=(HTML tag[attribute^=starting characters of the string])

Example: Locating the first element of the Div tag.

CSS Selector End with

It helps in locating elements that end with a particular value. we would use $= which means “end with”

Syntax:
css=(HTML tag[attribute$=ending characters of the string])

Example: Locating the second element of the Div tag.

CSS Selector Contains

It is used to locate an element by using a matching substring. To select the last div element, we would use *= which means “sub-string’

Syntax:
css=(HTML tag[attribute*=sub string])

Example: Locating the second element of the Div tag.

CSS :not() Selector with Multiple Classes

The :not() selector is used when you want to select an element that doesn’t have a certain class. Basically, it prevents specific items from being selected.

Suppose you have various elements on a page that have the same attribute and attribute value except some of them have additional values. See the below example.

As you can in the above tags all four divs contain “carrier-“ but the first two also contain “clearfix”.Suppose we don’t want to select the first two.

Example:

The CSS selector for Not selecting the first two divs is

Locating Child Element using nth-of-child

Child elements are used to locate elements inside other elements.

You can observe that the individual list elements don’t have any attribute value associated with them. To locate any of the list elements with a required text we have to use nth-of-type. For example, Selenium is the second element that can be located using the following code.

Example:

Likewise, we can also use :first-of-type and :last-child to select the first and last element.

Example:

Recommended Posts

Leave a Reply