Skip to content
CodeKruPro

Find Element by ID

4/70

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...

About this lesson

Selenium Java findElement with By.id: locate an element by its id

Selenium Java AssignmentsBeginnerLocators

How it works

By.id("value") matches the element whose id attribute equals that value. You pass it to driver.findElement(), which returns a WebElement you can read or act on. Selenium sends it to the browser as a CSS selector built from the id, which is why a failed lookup reports the css selector method in its error message.

HTML expects an id to be unique, and that is why id is usually the most dependable locator. The Text Fields page breaks the rule on purpose: field 1 and field 6 both carry the id firstName, and findElement() quietly returns the first one in document order.

If nothing matches, findElement() does not return null. It throws NoSuchElementException. When you only want to test whether something exists, call findElements() instead; it returns an empty List<WebElement> rather than throwing.

About the assignment

The Text Fields page has a last-name box that shows grey hint text before you type. You will locate that box by its id and read the hint, which lives in the field's placeholder attribute rather than in its visible text.

What you will do

  1. The page is already open. Build an id locator for lastNameWithPlaceholder with By.id().
  2. Pass it to driver.findElement() to get the WebElement.
  3. Read the placeholder attribute from that element.
  4. Print the value after the label Placeholder:.

Common mistakes

  • Calling getText() on an input

    An input has no inner text, so getText() returns an empty string for this field. The hint is an attribute; read it with getAttribute() or getDomAttribute().

  • Generated ids

    Some frameworks create ids such as ember412 or react-select-3-input that change on every build or page load. A locator built on one passes today and fails tomorrow with NoSuchElementException; pick a stable attribute instead.

  • Element inside an iframe

    The driver only searches the current frame. An element inside an iframe is not found by id until you switch into it with driver.switchTo().frame(), which lessons 44 to 47 cover.

In a real test suite

In a page object class, id locators are usually stored once as By fields and reused by every method, so a changed id is fixed in one place. To read an attribute exactly as written in the HTML, use getDomAttribute().

Not every element has an id. Lesson 5 locates the read-only field on this same page through its name attribute instead, the usual fallback for form controls.

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 element by ID "lastNameWithPlaceholder"
        // TODO: Get its "placeholder" attribute
        // TODO: Print in format: "Placeholder: <value>"
        
        driver.quit();
    }
}