Locators for selenium | name locator example | gmail login automation example in java-selenium

Sadakar Pochampalli
JasperSoft BI Suite Tutorials - Sadakar Pochampalli )

Hi,

In this post, you will see the demonstration of "name" locator usage. 

"name" is one of the 8 locators supported by selenium, using it one can navigate to target page by performing click action(s).

For instance, Login to gmail  @ https://mail.google.com/
Let's see how to identify the name locators of login elements for gmail web application.

Email or Phone input text input HTML with "name" locator 
<input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" 
       autocomplete="username" spellcheck="false" tabindex="0" 
       aria-label="Email or phone" name="identifier" value="" autocapitalize="none" 
       id="identifierId" dir="ltr" data-initial-dir="ltr" data-initial-value="">

Enter your password text input HTML with "name" locator
<input type="password" class="whsOnd zHQkBf" jsname="YPqjbf" 
       autocomplete="current-password" spellcheck="false" tabindex="0" 
       aria-label="Enter your password" name="password" autocapitalize="off" 
       dir="ltr" data-initial-dir="ltr" data-initial-value="">

Click on the image to get best view

selenium identifies the above input elements(username and password for gmail) using "name" locator with the following java statements. 
driver.findElement(By.name("identifier")).sendKeys("java.selenium2021@gmail.com");

WebElement elePassword=driver.findElement(By.name("password"));
elePassword
.sendKeys("JavaSelenium2021");
Take a look at the following example or watch this 1 min no voice video tutorial for end-to-end demo understanding and execution. 
nameLocatorDemo.java
//name locator demo
package selenium.locators.examples;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class nameLocatorDemo {

public static void main(String[] args) {

WebDriver driver
;

System
.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver.exe");
System
.setProperty("webdriver.chrome.silentOutput", "true");

driver
= new ChromeDriver();
driver
.navigate().to("https://mail.google.com/");

driver
.manage().window().maximize();

// locate "Email or Phone" text input with "name" locator and enter email say --> java.selenium2021@gmail.com

driver.findElement(By.name("identifier")).sendKeys("java.selenium2021@gmail.com");

// click on "Next" button - This is an xpath example that will be covered in later sessions
driver
.findElement(By.xpath("//span[@class='RveJvd snByac']")).click();

driver
.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//locate "Enter your password" text input with "password" locator and enter password say --> JavaSelenium2021
WebElement elePassword=driver.findElement(By.name("password"));
elePassword.sendKeys("JavaSelenium2021");


elePassword
.sendKeys(Keys.TAB);
elePassword
.sendKeys(Keys.TAB);

// click on "Next" button - This is again an xpath example.
driver
.findElement(By.xpath("//span[contains(text(),'Next')]")).click();

//close the driver
//driver.close();
}
}


- Sadakar Pochampalli

Feedback
randomness