By Shivam Singh, founder of TesterRank · Updated September 2026
CSS selectors are your bread and butter. They're fast, clean, and handle 80% of your locator needs. If you know CSS for stylesheets, you already know most of this. If not, you'll learn it in the next 10 minutes.
These are the building blocks: find by tag name, by #id and by .class. Most CSS selectors you write will start with one of them.
// By tag name
driver.findElement(By.cssSelector("h1"));
driver.findElement(By.cssSelector("input"));
driver.findElement(By.cssSelector("button"));
// By class name (prefix with .)
driver.findElement(By.cssSelector(".product-card"));
driver.findElement(By.cssSelector(".product-name"));
// By ID (prefix with #)
driver.findElement(By.cssSelector("#userId"));
driver.findElement(By.cssSelector("#password"));
// Tag + class
driver.findElement(By.cssSelector("button.add-to-cart"));
driver.findElement(By.cssSelector("input.qty-select"));
// Multiple classes (no space between dots)
driver.findElement(By.cssSelector(".btn.btn-primary"));
driver.findElement(By.cssSelector(".tab.active"));Square brackets match any attribute, such as [type='submit']. You can match the full value, or only its start, end or middle.
// Exact match: [attr='value']
driver.findElement(By.cssSelector("button[type='submit']"));
driver.findElement(By.cssSelector("[data-testid='loginBtn']"));
driver.findElement(By.cssSelector("input[name='password']"));
// Starts with: [attr^='value']
driver.findElement(By.cssSelector("[data-testid^='product-name']"));
// Matches: product-name-1, product-name-2, product-name-3...
// Ends with: [attr$='value']
driver.findElement(By.cssSelector("[data-testid$='-3']"));
// Matches: product-name-3, add-to-cart-3, product-price-3...
// Contains: [attr*='value']
driver.findElement(By.cssSelector("[data-testid*='filter']"));
// Matches: cat-filter-electronics, clear-filters...
// Has attribute (any value): [attr]
driver.findElement(By.cssSelector("[data-testid]"));
// Matches any element with a data-testid attributeCombinators describe how elements are related: a space means "anywhere inside", > means "direct child", and + or ~ mean "next to".
// Descendant (space) — child at ANY depth
driver.findElement(By.cssSelector(".product-item .product-name"));
driver.findElement(By.cssSelector("form input"));
// Child (>) — DIRECT child only
driver.findElement(By.cssSelector("form > button"));
driver.findElement(By.cssSelector("ul > li"));
// Adjacent sibling (+) — the NEXT sibling
driver.findElement(By.cssSelector("label + input"));
// General sibling (~) — ALL following siblings
driver.findElements(By.cssSelector("h2 ~ p"));By.className() only accepts a SINGLE class name. If an element has class="product-card active", using By.className("product-card active") throws InvalidSelectorException. Use By.cssSelector(".product-card.active") instead.
Read a CSS selector from right to left. The last part is the element you want; everything before it narrows the search. For example, form#login input[type='password'] means "a password input that is inside the form with id login". This habit helps you understand selectors other people wrote and spot why one matches the wrong element.
#id finds by id, .class finds by class, tag finds by tag name.[attr='value'] matches an attribute exactly; ^= means "starts with", $= "ends with", *= "contains".> means "direct child only".Open DevTools (F12), go to the Elements tab and press Ctrl+F. Type your CSS selector in the search box. DevTools highlights every match and shows a count like "1 of 1". If you see "1 of 3", your selector is not unique and Selenium will silently use the first match, which may be the wrong element.
Avoid selectors built only from styling classes such as .btn.btn-primary. Many buttons share those classes, and they change when the design changes.
Key Point: CSS selectors: # for ID, . for class, [attr=val] for attributes, space for descendant, > for direct child.