What is CSS Selector?
CSS Selectors are patterns used to target HTML elements based on tag name, attributes, hierarchy, position, or state.
In UI automation, CSS selectors are preferred for their speed, readability, and native browser support.
⥠Key Features
- Native browser execution (faster than XPath)
- Clean and readable syntax
- Strong attribute-based matching
- Widely supported across tools
đ¯ Use Cases
- Selenium WebDriver locators
- Playwright & Cypress automation
- Stable regression test suites
- Frontend validation & UI checks
đ CSS - ID Selector
Targets elements using a unique id attribute.
#loginButton
Example HTML
<button id="loginButton">Login</button>
When to use:
- ID is unique and stable
- Best-performing selector
đ¨ CSS - Class Selector
Targets elements using a class name.
.submit-btn
Example HTML
<button class="submit-btn">Submit</button>
When to use:
- Reusable components
- Styling-aligned selectors
đˇī¸ CSS - Tag Selector
Targets elements by HTML tag.
input
When to use:
- Generic selections
- Combined with attributes
đ CSS - Attribute Equals Selector
input[type="password"]
Example HTML
<input type="password" />
When to use:
- Form inputs
- Stable attributes
đ CSS - Attribute Contains (*=)
a[href*="login"]
Example HTML
<a href="/user/login">Login</a>
âļī¸ CSS - Attribute Starts With (^=)
input[id^="user_"]
âšī¸ CSS - Attribute Ends With ($=)
img[src$=".png"]
đŗ CSS - Descendant Selector
Targets elements inside another element.
form input
When to use:
- Nested DOM structures
- Scoped selections
đļ CSS - Direct Child Selector (>)
ul > li
âī¸ CSS - Adjacent Sibling Selector (+)
label + input
đĨ CSS - General Sibling Selector (~)
label ~ input
đ CSS - Multiple Selectors (OR Condition)
button.primary, button.secondary
1ī¸âŖ CSS - :first-child
li:first-child
đ CSS - :last-child
li:last-child
đĸ CSS - :nth-child()
tr:nth-child(2)
Note: Index starts from 1.
#ī¸âŖ CSS - :nth-of-type()
div:nth-of-type(3)
â CSS - :not() Selector
input:not([disabled])
When to use:
- Exclude disabled or hidden fields
â CSS - :checked Selector
input:checked
đī¸ CSS - :empty Selector
div:empty
đ CSS - Combined Attribute Strategy
input[type="text"][name="username"]
Why recommended:
- Highly stable
- Resistant to UI changes
â CSS - Data Attribute Selector (BEST PRACTICE)
button[data-testid="login-btn"]
Why preferred:
- Automation-safe
- No visual impact
- Stable across releases
â ī¸ CSS - Index Based Selection (Last Resort)
li:nth-child(3)
Avoid when:
- Dynamic lists
- Frequently changing UI
đ¨ CSS - Common Automation Pitfalls
â Dynamic classes (hash-based)
â Deep DOM chaining
â Index-heavy selectors
â Styling-only hooks
đ CSS - Recommended Locator Priority
- id
- data-* attributes
- stable attributes
- class names
- hierarchy selectors
- index-based selectors
âī¸ CSS vs XPath - Quick Comparison
| Feature | CSS | XPath |
|---|---|---|
| Performance | Fast | Slower |
| Readability | High | Medium |
| DOM Traversal Upwards | â No | â Yes |
| Native Browser Support | â Yes | â Partial |
đ§ CSS - Tool Compatibility
Supported by:
- Selenium WebDriver
- Playwright
- Cypress
- Puppeteer
- WebDriverIO