Skip to content
CodeKruPro

CSS Selectors - Basics

11/70

Video walkthrough

Watch on YouTube

CSS Selectors - Basics

CSS Selectors are powerful locators that use CSS syntax. Common patterns:

  • #id - find by ID
  • .class - find by class
  • tag - find by tag name
  • tag#id - find by tag with specific ID
  • tag.class - find by tag with specific class

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the pre-filled text field using CSS selector #preFilledTextField
  3. Print its value
  4. Find the disabled field using CSS selector input#disabledField
  5. Print its value

Expected output:

Pre-filled value: Codekru
Disabled value: Codekru

About this lesson

CSS selector by id and class in Selenium Java: #id, .class and tag combinations

Selenium Java AssignmentsBeginnerCSS and XPath Locators

How it works

By.cssSelector() takes the same selector syntax a stylesheet uses, and the browser evaluates it natively, so it is fast and reads naturally. #preFilledTextField means "id equals preFilledTextField", .last-name-input means "has that class", and a bare input means "any input element".

The real gain over By.id() and By.className() is that the parts combine. input.last-name-input requires both the tag and the class, .pt-1.pb-1 requires two classes on the same element (seven elements on Text Fields have both), and a space between two parts, as in #textFieldElements input, means "an input anywhere inside the element with that id". That one selector finds all seven inputs in the practice area.

Under the hood Selenium already turns By.id() and By.className() into CSS selectors before sending them to the browser, which is why a failed id lookup reports a css selector in its error message.

About the assignment

Two fields on Text Fields hold the same text: the pre-filled one, which you can edit, and the disabled one, which you cannot. You will locate each with a CSS selector, one by id alone and one by tag plus id, and read the text each holds.

What you will do

  1. Locate the pre-filled field with By.cssSelector() and the id selector the task gives you.
  2. An input keeps its text in its value, not in its visible text, so read it with getAttribute() and print it after the label Pre-filled value:.
  3. Locate the disabled field with the tag-plus-id selector from the task, read its value the same way and print it after the label Disabled value:.

Common mistakes

  • Leaving out the # or the dot

    Without a prefix, preFilledTextField is read as a tag name. No such tag exists, so findElement() throws NoSuchElementException.

  • Putting a space inside tag#id

    input #disabledField with a space means "an element with that id somewhere inside an input". Inputs have no children, so nothing matches and you get NoSuchElementException.

  • Writing a broken selector

    An unclosed bracket such as input[ is not a valid selector at all. Chrome rejects it and Selenium throws InvalidSelectorException with the message "An invalid or illegal selector was specified".

  • Trusting an id to be unique

    Ids should be unique, but this page has two inputs with id="firstName", and #firstName matches both. findElement() quietly returns the first, so check with findElements() when a page looks hand-built.

In a real test suite

Most teams use CSS selectors as their default locator: they are shorter than XPath, they match what front-end developers write, and you can test them in the browser console with document.querySelectorAll() before putting them in a test.

Notice that reading a disabled field works fine; only typing into it fails. Lesson 10 counted radios by tag name alone; lesson 12 shows how a CSS attribute selector narrows a match to inputs of one type, including matches on the start, end or middle of a value.

To try one of these calls on any page without a task, open the online Selenium compiler for Java.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/TextFields");
        
        // TODO: Find element using CSS selector #preFilledTextField
        // TODO: Print its value
        // TODO: Find element using CSS selector input#disabledField
        // TODO: Print its value
        
        driver.quit();
    }
}