CSS attribute and pseudo-class selectors help find web elements by their attributes or special states. This makes tests more precise and reliable.
CSS attribute and pseudo-class selectors in Selenium Java
driver.findElement(By.cssSelector("tagName[attribute='value']")); driver.findElement(By.cssSelector(":pseudo-class"));
Use square brackets [] to match attributes.
Pseudo-classes start with a colon (:), like :first-child or :checked.
driver.findElement(By.cssSelector("input[type='checkbox']"));driver.findElement(By.cssSelector("button:disabled"));driver.findElement(By.cssSelector("li:first-child"));driver.findElement(By.cssSelector("a[href*='login']"));This test opens a webpage and finds elements using CSS attribute and pseudo-class selectors. It prints confirmation and details for each found element.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class CssSelectorTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/form"); // Select checkbox input by attribute WebElement checkbox = driver.findElement(By.cssSelector("input[type='checkbox']")); System.out.println("Checkbox found: " + checkbox.isDisplayed()); // Select disabled button by pseudo-class WebElement disabledButton = driver.findElement(By.cssSelector("button:disabled")); System.out.println("Disabled button found: " + disabledButton.isDisplayed()); // Select first list item WebElement firstItem = driver.findElement(By.cssSelector("ul li:first-child")); System.out.println("First list item text: " + firstItem.getText()); // Select link containing 'login' in href WebElement loginLink = driver.findElement(By.cssSelector("a[href*='login']")); System.out.println("Login link href: " + loginLink.getAttribute("href")); } finally { driver.quit(); } } }
Always prefer unique attributes for selectors to avoid flaky tests.
Pseudo-classes like :hover cannot be triggered by Selenium directly; use Actions class for that.
Use *= in attribute selectors to find partial matches inside attribute values.
CSS attribute selectors match elements by attribute values.
Pseudo-class selectors match elements in special states or positions.
Using these selectors in Selenium helps find elements precisely and write stable tests.
Practice
<input> elements with a type attribute equal to "text"?Solution
Step 1: Understand attribute selectors
The selectorinput[type='text']matchesinputelements whosetypeattribute exactly equalstext.Step 2: Differentiate other selectors
^=means starts with,$=means ends with, and*=means contains. These do not require exact match.Final Answer:
input[type='text'] -> Option DQuick Check:
Exact attribute match = input[type='text'] [OK]
- Confusing ^= (starts with) with = (exact match)
- Using $= or *= when exact match is needed
- Forgetting to quote attribute values
<a> elements with an href attribute containing the word "login"?Solution
Step 1: Understand the *= attribute selector
The selectora[href*='login']matchesaelements whosehrefattribute contains the substringloginanywhere.Step 2: Differentiate other selectors
^=matches start,$=matches end, and=matches exact. We want contains, so*=is correct.Final Answer:
a[href*='login'] -> Option AQuick Check:
Contains substring = *= [OK]
- Using ^= or $= instead of *= for substring match
- Forgetting quotes around attribute values
- Using = which requires exact match
<ul> <li>Item 1</li> <li class="active">Item 2</li> <li>Item 3</li> <li class="active">Item 4</li> </ul>
What will the CSS selector
li.active:first-of-type select?Solution
Step 1: Understand
:first-of-typepseudo-class:first-of-typeselects the first sibling of its type (here, the firstli) regardless of class.Step 2: Combine with class selector
The selectorli.active:first-of-typelooks for anlithat is both.activeand the firstli. The firstlihas no class, so no match.Final Answer:
No elements, because first-of-type ignores class -> Option CQuick Check:
first-of-type ignores class, so no match [OK]
:first-of-type matches first element type ignoring classes [OK]- Assuming :first-of-type matches first element with class
- Confusing :first-child with :first-of-type
- Ignoring that class must match too
<button> elements that are disabled using a CSS selector in Selenium. Which selector is incorrect and will cause your test to fail?Solution
Step 1: Identify valid selectors for disabled buttons
button:disabledandbutton[disabled]correctly select disabled buttons.button[disabled='disabled']also works if attribute equals 'disabled'.Step 2: Spot invalid pseudo-class
:enableis not a valid CSS pseudo-class. The correct one is:enabled.Final Answer:
button:enable -> Option AQuick Check:
Invalid pseudo-class = :enable [OK]
- Using :enable instead of :enabled
- Assuming [disabled='true'] works (it must match exact value)
- Confusing attribute selector syntax
<input> elements with various name attributes:<input name="user_email"> <input name="user_password"> <input name="admin_email"> <input name="admin_password">
Which CSS selector will select only the
input elements whose name attribute ends with "_email"?Solution
Step 1: Understand $= attribute selector
The selectorinput[name$='_email']matchesinputelements whosenameattribute ends exactly with_email.Step 2: Differentiate other selectors
*=matches contains anywhere,^=matches starts with, and=matches exact. We want ends with, so$=is correct.Final Answer:
input[name$='_email'] -> Option BQuick Check:
Ends with substring = $= [OK]
- Using *= or ^= instead of $= for ends with
- Forgetting quotes around attribute values
- Using = which requires exact match
Ready to prove it?
Pass the test and this pattern moves unknown
