Skip to content
CodeKruPro

CSS Selectors - Attribute Selectors

12/70

Video walkthrough

Watch on YouTube

CSS Attribute Selectors

CSS attribute selectors match elements based on their attributes:

  • [attr] - has attribute
  • [attr='value'] - exact match
  • [attr^='value'] - starts with
  • [attr$='value'] - ends with
  • [attr*='value'] - contains

Relevant HTML structure:

<input type="text" id="lastNameWithPlaceholder"
       class="last-name-input"
       placeholder="Enter your last name..." name="lastName">

<input type="text" id="preFilledTextField"
       name="preFilledTextField" value="Codekru">

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the input with placeholder='Enter your last name...' using CSS attribute selector
  3. Print its ID
  4. Find the input whose name starts with pre using [name^='pre']
  5. Print its value

Expected output:

Found by placeholder: lastNameWithPlaceholder
Found by name prefix: Codekru

About this lesson

CSS attribute selector in Selenium Java: exact, starts-with, ends-with and contains matches

Selenium Java AssignmentsBeginnerCSS and XPath Locators

How it works

Square brackets in a CSS selector test an attribute. [readonly] only checks that the attribute is present, and [type='radio'] checks for an exact value. Three operators match part of a value: ^= for the start, $= for the end and *= for anywhere inside it.

On Text Fields, input[name$='Field'] matches three inputs and input[name*='Name'] matches another three, so partial matches are a way to find a group of related fields with one locator, or to pin down an element whose id carries a generated suffix.

Value matching is case-sensitive. A lowercase copy of the last-name placeholder matches nothing, while adding the i flag before the closing bracket makes the same comparison case-insensitive and finds the field.

About the assignment

Neither field in this task is located by its id. You find the last-name field through its placeholder text and the pre-filled field through the first letters of its name, then read a different attribute from each to prove you found the right one.

What you will do

  1. Build a selector that matches the placeholder attribute exactly. The value contains spaces and dots, so it must sit inside single quotes inside the Java string.
  2. Read the matched field's id with getAttribute() and print it after the label Found by placeholder:.
  3. Build a second selector with the ^= operator on the name attribute, using the prefix from the task.
  4. Read that field's value and print it after the label Found by name prefix:.

Common mistakes

  • Leaving the value unquoted

    Quotes are optional only for simple values. A placeholder with spaces in it, written without quotes, is an invalid selector and throws InvalidSelectorException.

  • Using double quotes inside the Java string

    A bare " ends the Java string early and the code does not compile. Use single quotes inside the selector, or escape the inner quotes as \".

  • Getting the case wrong

    An attribute value that differs only in case does not match, so findElement() throws NoSuchElementException. Copy the value from the page, or add the i flag.

  • Choosing a prefix that is too short

    A short prefix can match several fields. input[name^='first'] matches both firstName inputs on this page, and findElement() quietly returns the first one in document order.

In a real test suite

Attribute selectors shine when the most stable hook is not an id: data-testid and data-test attributes that teams add for automation, aria-label on icon buttons, or the name attribute that forms submit to the server.

Lesson 11 combined tag, id and class. CSS cannot match on an element's text, though, which is one reason XPath exists; lesson 13 introduces it.

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 input by placeholder attribute
        // TODO: Find input whose name starts with "pre"
        
        driver.quit();
    }
}