By Shivam Singh, founder of TesterRank · Updated September 2026
Pseudo-classes are words after a colon, like :first-child or :not(). They do not match an attribute; they match a position or a state. Think of them as filters: "from these rows, give me only the second one" or "from these inputs, skip the hidden ones".
Pseudo-classes let you select elements by position, state, or relationship. These are incredibly useful for tables, lists, and forms.
Pick an element by its position among its siblings, such as the first, last or third item in a list.
// :first-child — first child of its parent
driver.findElement(By.cssSelector("ul li:first-child"));
// :last-child — last child of its parent
driver.findElement(By.cssSelector("ul li:last-child"));
// :nth-child(n) — the nth child (1-based)
driver.findElement(By.cssSelector("table tr:nth-child(2)"));
// 2nd row of the table (often the first data row)
driver.findElement(By.cssSelector(".product-item:nth-child(3)"));
// 3rd product card
// :nth-child(odd) and :nth-child(even)
driver.findElements(By.cssSelector("table tr:nth-child(odd)"));
// 1st, 3rd, 5th rows...
// :nth-child(3n) — every 3rd item
driver.findElements(By.cssSelector("li:nth-child(3n)"));
// 3rd, 6th, 9th...Match elements by their state (checked, disabled, enabled), or leave some out with :not().
// :not() — exclude elements matching a selector
driver.findElements(By.cssSelector("input:not([type='hidden'])"));
// All visible inputs (excludes hidden ones)
driver.findElements(By.cssSelector(
".product-item:not(:first-child)"));
// All products except the first one
// :disabled and :enabled
driver.findElements(By.cssSelector("button:disabled"));
driver.findElements(By.cssSelector("input:enabled"));
// :checked — for selected checkboxes and radio buttons
driver.findElements(By.cssSelector(
"input[type='checkbox']:checked"));Here are working selectors for the TesterRank banking portal. Open the page in your browser and try each one in DevTools as you read.
// Banking Portal Login Page
driver.get("https://www.testerrank.com/banking/login");
WebElement userId = driver.findElement(
By.cssSelector("[data-testid='userId']"));
WebElement password = driver.findElement(
By.cssSelector("[data-testid='password']"));
WebElement loginBtn = driver.findElement(
By.cssSelector("[data-testid='loginBtn']"));
WebElement otpLoginBtn = driver.findElement(
By.cssSelector("[data-testid='otpLoginBtn']"));
WebElement rememberMe = driver.findElement(
By.cssSelector("[data-testid='rememberMe']"));
WebElement registerLink = driver.findElement(
By.cssSelector("[data-testid='registerLink']"));
// Banking Portal Dashboard
driver.get("https://www.testerrank.com/banking/dashboard");
WebElement welcomeUser = driver.findElement(
By.cssSelector("[data-testid='welcomeUser']"));
WebElement balance = driver.findElement(
By.cssSelector("[data-testid='accBal1']"));
WebElement firstTxn = driver.findElement(
By.cssSelector("[data-testid='txnRow-0']"));
WebElement fundTransfer = driver.findElement(
By.cssSelector("[data-testid='qa-FundTransfer']"));And the same idea on the shopping portal: selectors for the product list, filters and cart.
// Shopping Portal Catalog Page
driver.get("https://www.testerrank.com/shopping/catalog");
WebElement searchBox = driver.findElement(
By.cssSelector("[data-testid='search-box']"));
WebElement clearFilters = driver.findElement(
By.cssSelector("[data-testid='clear-filters']"));
WebElement electronicsFilter = driver.findElement(
By.cssSelector("[data-testid='cat-filter-electronics']"));
WebElement priceMin = driver.findElement(
By.cssSelector("[data-testid='price-min']"));
WebElement priceMax = driver.findElement(
By.cssSelector("[data-testid='price-max']"));
WebElement resultCount = driver.findElement(
By.cssSelector("[data-testid='result-count']"));
// All product cards
List<WebElement> allProducts = driver.findElements(
By.cssSelector("[data-testid='product-item']"));
// Specific product by ID
WebElement product1Name = driver.findElement(
By.cssSelector("[data-testid='product-name-1']"));
WebElement addToCart1 = driver.findElement(
By.cssSelector("[data-testid='add-to-cart-1']"));Test your CSS selectors before putting them in code. Open DevTools (F12), go to Console, type: document.querySelector("your-selector"). If it highlights the right element, you're good. For multiple elements: document.querySelectorAll("your-selector").length.
li:nth-child(2) means "the second child of its parent, but only if it is an li". If the first child is a heading or a div, the count still includes it, and your selector may match nothing. li:nth-of-type(2) counts only li elements, so it is often what beginners actually want. When a position selector returns nothing, this difference is usually the reason.
Position selectors break as soon as the order changes, for example when a table is sorted or a new product is added at the top. Use them for things that really are about position, such as "the first row is the header" or "check every odd row". To find a specific record, match it by its text or data attribute instead.
Q: How would you select all enabled, visible inputs in a form except hidden fields?
A: Use a CSS selector with negation and state: form input:enabled:not([type='hidden']). Then, in Java, filter the list with isDisplayed() if some inputs are hidden by CSS rather than by type.
Key Point: :nth-child for position, :not for exclusion, :disabled/:checked for state. Test selectors in DevTools before coding.