By.xpath() method

Xpath plays very important role to drive the browser for automation.  XPATH extension is XML Path.
All this web page is form of XML document.  To find the web element on the web page, user can use thsi XPATH locator mechanism.

To find the XPath of the web element, we can use the Firepath extension/plug-in for firefox browser.

How it works to locate:-

1.  To locate all the 
elements : //div
2.  To locate the root element : //
3.  To locate the link tags inside the element : //div/a
4.  To locate all the tags : //div/*
5.  To locate the elements from two or three levels down : //*/*/div
6.  To locate the specific element : //*/div/a[@id='Attribute Value'], For 
Example, facebook input id for email login will be : //*[@id='email']

Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.chrome.*;

public class linktext {
 
private static WebDriver driver = null;
 
public static void main(String[] args){

//setting the property for chrome browser and to invoke the chrome browser
System.setProperty("webdriver.chrome.driver", "C://Selenium//chromedriver.exe");  
WebDriver driver=new ChromeDriver();

driver.get("http://www.facebook.com");

//Retrieve and type some keys in email id login using XPATH
WebElement fbEmailInputXpath = driver.findElement(By.xpath("//*[@id='email']"));

fbEmailInputXpath.sendKeys("xxxxx@gmail.com");

//Printing the result into the console
System.out.println("Successfully entered the email id at email id input " +fbEmailInputXpath);
}
}
Output:
Successfully entered the email id at email id input 
[[ChromeDriver: chrome on XP (01d231097bed66c336cc3fa5a7577226)] ->
xpath: //*[@id='email']]
Socialize It and Share the post with your friends
SOCIALIZE IT →
FOLLOW US →
SHARE IT →