Skip to content
CodeKruPro

Locate an element by one class name

Automation taskEasyAsked in: Accenture, Capgemini, TCS, Wipro

A follow-up trap: "Can the class name locator take two classes separated by a space?" It cannot; it takes exactly one class. For an element with two classes, write a CSS selector such as .btn.primary. Here you find a field by its class name and print its id.

Find Element by Class Name

Video walkthrough

Watch on YouTube

Find Element by Class Name

The By.className() locator finds an element using its CSS class. Note: you can only use one class name (no spaces).

On the Text Fields page, the "Last Name With Placeholder" field has the class last-name-input.

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the element by class name last-name-input
  3. Print its ID attribute

Expected output:

Element ID: lastNameWithPlaceholder