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
- Build a selector that matches the
placeholderattribute exactly. The value contains spaces and dots, so it must sit inside single quotes inside the Java string. - Read the matched field's
idwithgetAttribute()and print it after the labelFound by placeholder:. - Build a second selector with the
^=operator on thenameattribute, using the prefix from the task. - Read that field's
valueand print it after the labelFound 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()throwsNoSuchElementException. Copy the value from the page, or add theiflag.Choosing a prefix that is too short
A short prefix can match several fields.
input[name^='first']matches bothfirstNameinputs on this page, andfindElement()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.
Related lessons
- CSS Selectors - BasicsThe id, class and tag parts that attribute tests are added to.
- XPath Functions - contains() and starts-with()XPath's
contains()andstarts-with()do the same partial matching, and also work on text. - Get Attribute Value with getAttribute()Read any attribute back once you have found the element.
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.
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();
}
}