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
- Locate the pre-filled field with
By.cssSelector()and the id selector the task gives you. - An input keeps its text in its
value, not in its visible text, so read it withgetAttribute()and print it after the labelPre-filled value:. - Locate the disabled field with the tag-plus-id selector from the task, read its
valuethe same way and print it after the labelDisabled value:.
Common mistakes
Leaving out the # or the dot
Without a prefix,
preFilledTextFieldis read as a tag name. No such tag exists, sofindElement()throwsNoSuchElementException.Putting a space inside tag#id
input #disabledFieldwith a space means "an element with that id somewhere inside an input". Inputs have no children, so nothing matches and you getNoSuchElementException.Writing a broken selector
An unclosed bracket such as
input[is not a valid selector at all. Chrome rejects it and Selenium throwsInvalidSelectorExceptionwith 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#firstNamematches both.findElement()quietly returns the first, so check withfindElements()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.
Related lessons
- Find Element by Class Name
By.className()takes one class; a CSS selector can require several. - CSS Selectors - Attribute SelectorsMatch on any attribute, not only id and class.
- XPath - BasicsXPath, the other general-purpose locator, compared on the Buttons page.
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 element using CSS selector #preFilledTextField
// TODO: Print its value
// TODO: Find element using CSS selector input#disabledField
// TODO: Print its value
driver.quit();
}
}