Quantcast
Channel: Pochampalli IT Labs
Viewing all articles
Browse latest Browse all 261

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

$
0
0

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
    <inputtype="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;
    importjava.util.concurrent.TimeUnit;
    importorg.openqa.selenium.By;
    importorg.openqa.selenium.Keys;
    importorg.openqa.selenium.WebDriver;
    importorg.openqa.selenium.WebElement;
    importorg.openqa.selenium.chrome.ChromeDriver;

    publicclassxpathLocatorGmailLoginDemo{

    publicstaticvoidmain(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();
    }
    }


    Viewing all articles
    Browse latest Browse all 261

    Trending Articles



    <script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>