Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

In Selenium, the By.name locator is used to find web elements using the value of their name attribute. This is particularly useful when dealing with form elements such as input fields, dropdowns, and other elements where the name attribute is commonly used.

Syntax

The ‘By.name‘ locator is used with the ‘findElement‘ or ‘findElements‘ method in the following way:

				
					WebElement element = driver.findElement(By.name("elementName"));

				
			

Example

Here’s an example demonstrating how to use the ‘By.name‘ locator in a Selenium test script written in Java:

  • Setting Up the WebDriver: Make sure you have the WebDriver executable for the browser you are testing (e.g., ‘chromedriver‘ for Chrome).

  • Using ‘By.name: Locate the element using its ‘name‘ attribute and perform actions on it.

Full Example Code

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

public class SeleniumByNameExample {
    public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize a new WebDriver instance
        WebDriver driver = new ChromeDriver();

        // Open a website
        driver.get("https://example.com");

        // Locate an element using the name attribute
        WebElement elementByName = driver.findElement(By.name("elementName"));

        // Perform actions on the located element
        elementByName.sendKeys("sample text");

        // Optionally, you can locate multiple elements with the same name attribute
        List<WebElement> elementsByName = driver.findElements(By.name("elementName"));
        for (WebElement element : elementsByName) {
            // Perform actions on each element
            element.click();
        }

        // Close the browser
        driver.quit();
    }
}

				
			

Breakdown of the Example

  1. Setting the System Property: The System.setProperty line sets the path to the WebDriver executable (in this case, chromedriver).

  2. Initializing WebDriver: new ChromeDriver() initializes a new instance of the Chrome browser.

  3. Opening a Website: driver.get("https://example.com") navigates to the specified URL.

  4. Locating the Element: driver.findElement(By.name("elementName")) locates the first element with the specified name attribute.

  5. Interacting with the Element: elementByName.sendKeys("sample text") sends text input to the located element.

  6. Locating Multiple Elements: driver.findElements(By.name("elementName")) returns a list of all elements with the specified name attribute.

  7. Iterating and Interacting with Each Element: The for loop iterates through each element in the list and performs actions on them.

  8. Closing the Browser: driver.quit() closes the browser window and terminates the WebDriver session.

Conclusion

The ‘By.name‘ locator is a straightforward and effective way to locate elements on a web page using their ‘name‘ attribute in Selenium with Java. This approach is commonly used in form handling and other scenarios where elements are identified by their ‘name‘ attributes.

Scroll to Top