Skip to content
CodeKruPro

Locate a field by its id

Automation taskEasyAsked in: Accenture, Capgemini, TCS, Wipro

The classic opener: "Which locator do you use first, and why?" An id should be unique on the page, it is short to write and easy to read, so it is the usual first choice. Find the field by its id and read one of its attributes (an attribute is a named value on an HTML element, such as placeholder="Email").

Find Element by ID

Video walkthrough

Watch on YouTube

Find Element by ID

The By.id() locator finds an element using its HTML id attribute. It's the fastest and most reliable locator.

On the Text Fields page, there is a field with:

  • ID: lastNameWithPlaceholder
  • Placeholder: Enter your last name...

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the element with ID lastNameWithPlaceholder
  3. Get its placeholder attribute value
  4. Print the result

Expected output:

Placeholder: Enter your last name...