Skip to content
CodeKruPro

Find Element by Name

5/70

Video walkthrough

Watch on YouTube

Find Element by Name

The By.name() locator finds an element using its name HTML attribute.

On the Text Fields page, the readonly field (#4) has:

  • Name: uneditable
  • Value: Codekru
  • It is readonly

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Find the element by name uneditable
  3. Print its value and tag name

Expected output:

Value: Codekru
Tag: input

About this lesson

By.name locator in Selenium Java: find a form field by its name attribute

Selenium Java AssignmentsBeginnerLocators

How it works

By.name("value") matches elements whose name attribute equals the value exactly. Form controls carry a name because it is the key the browser uses when the form is submitted, so input, select and textarea elements often have one even when developers never added an id.

Unlike an id, a name does not have to be unique. Radio buttons in one group deliberately share a name, and on the Text Fields page both first-name boxes use firstName. With a shared name, findElement() returns the first match, so check with findElements() how many elements a name really selects before you rely on it.

getTagName() tells you what kind of element a locator found. It returns the tag in lower case, which is a quick sanity check that a broad locator landed on the control you meant.

About the assignment

Field 4 on Text Fields is read-only: it shows a fixed value and ignores typing. It has the name uneditable. You will find it through that name, then report both its current value and the HTML tag behind it.

What you will do

  1. Locate the field with driver.findElement() and By.name().
  2. Read its current value from the value attribute and print it after the label Value:.
  3. Ask the element for its tag with getTagName() and print that after the label Tag:.
  4. Print the value line first and the tag line second, as the task lists them.

Common mistakes

  • Typing into a read-only field

    Calling sendKeys() on this field does not throw; Chrome simply ignores the keys and the value stays as it was. Calling clear() on it does fail, with InvalidElementStateException: invalid element state.

  • Confusing name with id

    Here the two happen to match, but on many pages they differ: the last-name box has the id lastNameWithPlaceholder and the name lastName. By.name() with an id value finds nothing and throws NoSuchElementException.

  • Expecting upper-case tag names

    JavaScript's element.tagName reports INPUT for HTML elements, but getTagName() returns the lower-case name, so a comparison against the upper-case form fails.

In a real test suite

Name locators suit server-rendered forms such as login pages, where username and password names are part of the contract with the back end and rarely change. They are weaker on pages that repeat the same form several times.

Lesson 6 moves from attributes that identify one control to a styling hook, the class attribute, which many elements can share.

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 name "uneditable"
        // TODO: Print its value attribute
        // TODO: Print its tag name
        
        driver.quit();
    }
}