December 2, 2023
How to find and Locate CSS Selector In Selenium

Cascading Style Sheets (CSS) are the backbone of web design, allowing you to control the presentation and layout of your web pages. To apply styles effectively, you need to understand CSS selectors – the keys that unlock the styling potential of your web elements. In this guide, we will explore various methods to find CSS selectors, helping you take your web design skills to the next level.

The usage of CSS selectors to identify web items in Selenium test scripts will be covered in this blog along with examples. The Selenium syntax for CSS selectors will also be shown.

Before focusing on CSS selectors in particular, it is advised to have a deeper understanding of Selenium locators.

Types of CSS Selectors in Selenium:

There are five types of CSS Selectors in Selenium tests:

  1. ID
  2. Class
  3. Attribute
  4. Sub-String
  5. Inner String

Why Knowing CSS Selectors Matters

Before diving into how to find CSS selectors, let’s briefly understand why they are crucial:

  1. Precision: CSS selectors enable you to target specific HTML elements or groups of elements, allowing for precise styling.
  2. Efficiency: Knowing how to find selectors efficiently saves time and helps you write cleaner, more maintainable code.
  3. Responsiveness: Proper use of selectors ensures your styles adapt gracefully to various screen sizes and devices.

Let’s explore different ways to find CSS selectors:

1. Browser Developer Tools

Modern web browsers come equipped with developer tools that are a goldmine for finding CSS selectors. Here’s how you can use them:

a. Right-click and Inspect: Right-click on the element you want to style and select “Inspect” from the context menu. This opens the developer tools, highlighting the corresponding HTML and showing you the applied CSS rules.

b. Element Selector: Hover over the HTML code within the developer tools to see the element’s properties. Clicking on an element will reveal its associated CSS selectors on the right-hand side.

c. Computed Styles: In the developer tools, the “Computed” or “Styles” tab will display all applied styles, including their respective selectors.

2. HTML Structure and Classes

Understanding your page’s HTML structure is fundamental to finding CSS selectors. Here are some key methods:

a. Element Type: To target all instances of a specific HTML element, use its tag name as a selector. For instance, p selects all paragraphs.

b. Class Selector: Add a class attribute to your HTML elements and select them using a class selector, e.g., .button.

c. ID Selector: For unique elements, use an ID attribute and select them using #, like #header.

3. Parent-Child Relationships

Sometimes, you need to style an element within a specific context. Utilize parent-child relationships to do this:

a. Descendant Selector: Select elements that are descendants of another element, e.g., article p selects all <p> elements within an <article>.

b. Child Selector: Target direct children of an element using >, like ul > li.

4. Attribute Selectors

If your elements have certain attributes, you can target them using attribute selectors:

a. Attribute Equals Selector: Select elements with a specific attribute value, e.g., [type="text"] selects all text input fields.

b. Attribute Contains Selector: Choose elements with attributes containing a specific value, e.g., [href*="example"] selects all links containing “example.”

5. Pseudo-Classes and Pseudo-Elements

Pseudo-classes and pseudo-elements allow you to select elements based on their state or position:

a. :hover: Style elements when hovered over, e.g., a:hover for link hover effects.

b. ::before and ::after: Create virtual elements for added styling, e.g., .quote::before.

6. Specificity and Inheritance

Understanding specificity and inheritance is essential for resolving conflicts and ensuring your styles apply correctly. Always consider these factors when finding selectors.


Mastering CSS selectors is a fundamental skill for web designers and developers. By using browser developer tools, understanding your HTML structure, and exploring various selector types, you can enhance your ability to create visually appealing and responsive web pages. Remember that practice is key to becoming proficient in finding and using CSS selectors effectively.

Visit Website

Youtube Channel

Contact US





Leave a Reply

Your email address will not be published. Required fields are marked *