Thursday, 21 May 2020

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


XPath
  • XPath = XML Path
  • It is a type of query language to identify any element on the web page. 
  • Syntax
    //tagname[@attribute='value']
    
  • Writing XPath for complex applications sometimes cumbersome task.
  • There are plenty of plug-ins available for chrome to find Xpaths and one such tool is  "Chropath" pulg-in. 
  • Take a look at the below image to find relative XPath for "Email or Phone" text input.
    Tap on the image to get better visibility of  content
Let's understand with an example, Identify gmail's  "Email or phone" text input with xpath
Tap on the image to get better visibility:

HTML
<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="">

from the above HTML,  tagname is input  | type is an attribute  | value is email

Xpath to locate the "Email or phone" is
//input[@type='email']

Java-selenium identifies the element with the following xpath locator
driver.findElement(By.xpath("//input[@id='identifierId']"))

Watch this ~4 min video tutorial example for automating gmail login process in which use xpath locators for "Email or Phone" or "Password"  or "Next" buttons. 


java-selenium code to identify gmail login using xpath locators
xpathLocatorGmailLoginDemo.java
//x-path locator example

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 xpathLocatorGmailLoginDemo {

 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.manage().window().maximize();
  
  driver.navigate().to("https://mail.google.com/");
  
  //locate "Email or Phone" text box input using "xpath" and enter email 
  driver.findElement(By.xpath("//input[@id='identifierId']")).sendKeys("java.selenium2021@gmail.com");
  
  // locate "Next" button and click 
  driver.findElement(By.xpath("//span[@class='RveJvd snByac']")).click();
  
  driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
  
  //locate "Enter your password" text box input using "xpath" and enter password
  WebElement elePassword=driver.findElement(By.xpath("//input[@name='password']"));
  elePassword.sendKeys("JavaSelenium2021");
  
  elePassword.sendKeys(Keys.TAB); 
  elePassword.sendKeys(Keys.TAB);
  
  // locate "Next" button and click 
  driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();
  
  //driver.close();
 }
}

No comments:

Post a Comment