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 SelectorWebElement 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 SelectorWebElement 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 css SelectorWebElement 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
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
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.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING