• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Selenium » Free Selenium Tutorial » Name Locator

Name Locator

Last Updated on: August 18, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Name Locator

In this previous post, we have seen different locators in selenium and how you can locate elements using ID locator. In this post, we are going to discuss “How to locate the elements using Name Locators”. You can find the below links of different locators to access the elements in a web page.

  • Locate element By Using ID Locator.
  • Locate element By Using Tag Name Locator.
  • Locate element By Using Link Text & Partial Link Text Locator.
  • Locate element By Using CSS Selector Locator.
  • Locate element By Using XPath Locator.

Now, let’s start learning how we can locate the elements using the Name locators.

Name Locator

If the ID locator details not present with an element attribute or you can not be able to locate the element because of matching ID of multiple elements, then that time we try to find the element with the help of Name locator. This is also efficient for locating the elements with the name attribute. But likewise ID, name locator are sometimes not unique on the web page. The only difference is that you should use the name locator instead of ID.

Note: If you have used this locator details in your script, and with that locator details you see multiple matching elements or Node (IN DOM elements also called as nodes), then when you will run your script that time the first element with the name attribute matching location will be returned. If with the attribute details no element has matched then that time you will get a NoSuchElementException.

Now let’s see how the Name locators work with the help of a real-time example:

Name Locator OrangeHRM
Name Locator OrangeHRM

Scenario:

  • Launch Mozilla Firefox
  • Open OrangeHRM On Firefox Browser
  • Locate the Enter Username & Password Text box and clear the existing value and then enter the username and password with using the Name locator.
  • Click Login Button

Code snippet:

package com.selenium.practice.locator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NameLocatorExample 
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.gecko.driver","Path Of Browser Driver");

      driver=new FirefoxDriver();
      driver.get("https://orangehrm-demo-6x.orangehrmlive.com/auth/login");
      Thread.sleep(10000);
      
      driver.findElement(By.name("txtUsername")).clear();
      driver.findElement(By.name("txtPassword")).clear();

      driver.findElement(By.name("txtUsername")).sendKeys("Admin");
      driver.findElement(By.name("txtPassword")).sendKeys("admin");
      driver.findElement(By.name("Submit")).click();
      
      driver.close();
   }
}

When we are run the above code, then it will give the same result as the ID locator. Let us know if you have found something interesting in this topic and your suggestions to improve the quality of the article in the comment section. So that we can try to update our article in that way.

    Filed Under: Free Selenium Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers