0
0
Selenium Javatesting~5 mins

findElement by name in Selenium Java

Choose your learning style9 modes available
Introduction

We use findElement by name to locate a web page element using its name attribute. This helps us interact with that element in tests.

When you want to fill a form field identified by its name attribute.
When you need to click a button or link that has a unique name attribute.
When you want to verify the presence or value of an input element by its name.
When other locators like id or class are not available but name is unique.
When automating simple web pages where name attributes are reliable.
Syntax
Selenium Java
WebElement element = driver.findElement(By.name("elementName"));

Replace "elementName" with the actual name attribute value of the element.

This returns the first matching element with that name on the page.

Examples
Finds the input field where the name attribute is username.
Selenium Java
WebElement usernameField = driver.findElement(By.name("username"));
Finds the search box element with name q, common in search forms.
Selenium Java
WebElement searchBox = driver.findElement(By.name("q"));
Finds a button or input element named submit to click or check.
Selenium Java
WebElement submitButton = driver.findElement(By.name("submit"));
Sample Program

This test script opens a login page, finds username and password fields by their name attributes, fills them, and clicks the login button. It prints a success message if all elements are found and clicked.

Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class FindElementByNameExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        try {
            driver.get("https://example.com/login");
            WebElement username = driver.findElement(By.name("username"));
            username.sendKeys("testuser");
            WebElement password = driver.findElement(By.name("password"));
            password.sendKeys("mypassword");
            WebElement loginButton = driver.findElement(By.name("login"));
            loginButton.click();
            System.out.println("Login form submitted successfully.");
        } catch (Exception e) {
            System.out.println("Element not found or error occurred.");
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Always ensure the name attribute is unique or you get the first matching element only.

If the element is not found, Selenium throws a NoSuchElementException.

Use explicit waits if the element loads dynamically to avoid errors.

Summary

findElement by name locates elements using their name attribute.

It is useful for form inputs and buttons with unique names.

Remember to handle exceptions if elements are missing.