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
- Locate the field with
driver.findElement()andBy.name(). - Read its current value from the
valueattribute and print it after the labelValue:. - Ask the element for its tag with
getTagName()and print that after the labelTag:. - 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. Callingclear()on it does fail, withInvalidElementStateException: 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
lastNameWithPlaceholderand the namelastName.By.name()with an id value finds nothing and throwsNoSuchElementException.Expecting upper-case tag names
JavaScript's
element.tagNamereportsINPUTfor HTML elements, butgetTagName()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.
Related lessons
- Find Element by IDThe id locator, the first choice when an element has a unique id.
- Handle Radio ButtonsRadio buttons in one group share a
name, which is what makes them a group. - Clear and Replace TextWhat happens when you clear and retype a field that is not read-only.
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 by name "uneditable"
// TODO: Print its value attribute
// TODO: Print its tag name
driver.quit();
}
}