Open In App

Selenium CSS Selectors

Last Updated : 24 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Selenium automation, CSS selectors are among the most powerful and flexible tools for locating web elements. They are especially useful when dealing with complex or dynamically changing HTML structures. In fact, around 75% of automation testers prefer CSS selectors over other locator strategies due to their speed, readability, and precision. In this article, we’ll break down the different types of CSS selectors available in Selenium using Java and walk you through practical examples to help you implement them effectively in your test scripts.

What is a CSS Selector?

A CSS Selector is a pattern used to select and style elements within an HTML document. In the context of Selenium, CSS selectors allow you to locate elements on a webpage for automation. CSS selectors are generally faster than other locators like XPath and can be more concise, making them a preferred choice for many automation testers. However, their reliability can depend on the specific context and the complexity of the HTML structure.

1. ID Selector

The ID Selector is one of the most straightforward and commonly used CSS selectors. It selects an element based on its unique ID attribute. Since IDs are unique within a webpage, this selector is highly reliable.

Syntax:

WebElement element = driver.findElement(By.cssSelector("#elementID"));

Example:

ID-css-Selector
ID css Selector

WebElement loginButton = driver.findElement(By.cssSelector("#loginBtn"));

loginButton.click();

In this example, the CSS selector #loginBtn targets the element with the ID loginBtn.

2. Class Selector

The Class Selector is used to select elements based on their class attribute. Unlike IDs, classes are designed to be reusable across multiple elements, so a class selector can target multiple elements.

Syntax:

WebElement element = driver.findElement(By.cssSelector(".className"));

Example:

Class-css-Selector
Class css Selector

WebElement menuOption = driver.findElement(By.cssSelector(".menu-item"));

menuOption.click();

Here, the selector .menu-item selects any element with the class menu-item.

3. Attribute Selector

The Attribute Selector allows you to target elements based on the presence or value of their attributes. This selector is particularly useful when you need to be more specific than just using an ID or class.

Syntax:

WebElement element = driver.findElement(By.cssSelector("[attribute='value']"));

Example:

Attribute-Selector
Attribute css Selector

WebElement emailField = driver.findElement(By.cssSelector("input[type='email']"));

emailField.sendKeys("[email protected]");

This example selects an input element where the type attribute equals email.

4. Combining Attributes

Sometimes, you need to combine multiple attributes to pinpoint an element accurately. Combining attributes in CSS selectors gives you the flexibility to be as specific as needed.

Syntax:

WebElement element = driver.findElement(By.cssSelector("tagName[attribute='value'][attribute2='value2']"));

Example:

Attribute-combiner
Attribute combiner


WebElement submitButton = driver.findElement(By.cssSelector("button[type='submit'][name='login']"));

submitButton.click();

In this case, the selector targets a button element that has both type='submit' and name='login' attributes.

5. Substring Selector

Substring Selectors are advanced CSS selectors that allow you to match elements based on partial attribute values. This is particularly useful when dealing with dynamic attributes.

Substring-selector
Substring Selector


Syntax:

WebElement element = driver.findElement(By.cssSelector("tagName[attribute^='startValue']"));

WebElement element = driver.findElement(By.cssSelector("tagName[attribute$='endValue']"));

WebElement element = driver.findElement(By.cssSelector("tagName[attribute*='subString']"));

Example:

WebElement partLink = driver.findElement(By.cssSelector("a[href*='partial-link']"));

partLink.click();


Here, the selector targets an a tag (anchor) whose href attribute contains the substring partial-link. The ^= operator selects elements where the attribute value starts with the specified substring, $= selects elements where the attribute value ends with the substring, and *= selects elements where the attribute value contains the substring.

Conclusion

CSS selectors are an essential tool for identifying and interacting with web elements in Selenium automation. By mastering these selectors, you can write more efficient and reliable test scripts. Whether you're dealing with simple IDs or complex combinations of attributes, CSS selectors provide the precision you need for effective automation.

Explore these examples in your projects to get hands-on experience, and you'll quickly see the power and flexibility of CSS selectors in Selenium WebDriver.


Next Article
Article Tags :

Similar Reads