In the previous tutorial, we have familiarized the basic syntax of XPath, absolute and relative XPath in Selenium. I hope that you have learned findElement commands, locators in Selenium. 

If not, I will recommend that you first go to these chapters, then come to learn how to find element by XPath in Selenium using get commands, WebElement commands and different locators.


By the end of this tutorial, you will able to:
  • Instantiate a WebDriver object to launch web browser.
  • Find element by XPath in Selenium
  • Work with the most frequently used method like get(), findElement(), sendKeys(), click(), close() of the WebDriver class.

How to Find Element by XPath in Selenium?


A user requires to find the location of web element to interact with a web page. So, to locate a web element on web page, Selenium WebDriver has provided two WebElement commands such as findElement, and findElements. 

Basically, findElement() method helps to uniquely identify a web element within the web page whereas findElements() method is used to identify the list of web elements within web page. Both have the following syntax as given below:

Syntax:
    1. WebElement elementName = driver.findElement(By.locatorName("Attribute value")); // Identify one web element within web page. 2. List<WebElement> elementName = driver.findElements(By.locatorName("Attribute value")); // Identify list of web elements within web page.
You can see in the above syntax that Find Element command takes an input parameter By object and returns an object of type WebElement. By is an object of By class which can be used with various locator strategies such as ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, and XPATH.

Attribute value of locator is a unique value that helps to locate or identify a web element on the web page. Therefore, developers and testers have the responsibility to make sure before identifying an attribute value whether it can uniquely identify web elements using certain locators such as ID or name or not.

For example:
    WebElement userName = driver.findElement(By.id("username"));
Here, username is an attribute value of locator id. Whereas, userName is a variable of type WebElement that is used to store a returning object of type WebElement.


Let's automate the following scenarios based on get command, WebElement commands, and locators.

Scenarios to Automate: 
There are the following steps to complete to automate the above scenario. They are as follows:
1. Launch the Firefox browser by creating a driver object using WebDriver reference variable. 2. Open the specified web application URL https://www.facebook.com. 3. Locate email and password elements on the web page. 4. Locate the login element on the web page and click on the login button.
5. Close the browser.

Now follow all the important steps to find element by XPath in Chrome Browser.
1. Open URL in the Chrome browser.
2. Right-click on the element on which you are trying to find element by XPath and then go to option "inspect".

3. Now, the Elements panel will be opened right-hand side on the screen. It will show in blue color.
4. Press ctrl+F to enable DOM searching in the panel.
5. Now you can write XPath in the panel to evaluate for that element.
6. If XPath is matched with any elements, they will be highlighted in DOM in yellow color. 

If you have any problem to find element by XPath in Chrome, go to the below link.

XPath of Element "email":

    WebElement email = driver.findElement(By.xpath("//input[@id='email']"));
Similarly, XPath of Element "Password":


    WebElement password = driver.findElement(By.xpath("//input[@id='pass']"));
XPath of Element "Login button":
    WebElement login = driver.findElement(By.xpath("//input[@id='u_0_2']"));
Let's start coding to automate the following scenarios. Look at the following source code.
Program source code 1:
    package seleniumProject; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class FacebookHomePage { public static void main(String[] args) { // Declare reference variable of type WebDriver. WebDriver driver; // Create a driver object of Firefox using WebDriver reference. driver=new FirefoxDriver(); // Maximize the browser. driver.manage().window().maximize(); // Declare Facebook home page URL using a variable of type string. String URL = "https://www.facebook.com"; // Call get() method using reference variable driver and pass the URL as a parameter. driver.get(URL); // It returns nothing. // Wait to load whole web page. driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Find elements by XPath using findElement command. WebElement email=driver.findElement(By.xpath("//input[@id='email']")); // Enter your login email id in box using sendKeys() method. email.sendKeys("deepak_cool70@yahoo.com"); WebElement password = driver.findElement(By.xpath("//input[@id='pass']")); // Enter your login password. password.sendKeys("d5516ak08#"); WebElement login = driver.findElement(By.xpath("//input[@type='submit']")); login.click(); System.out.println("Login successfully"); // Now close the browser. driver.close(); } }
    Output: Login successfully
Explanation:
1. Import the minimum libraries to run selenium script via WebDriver, WebElement, and FirefoxDriver:
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver;

2. When the statement WebDriver driver=new FirefoxDriver() will be executed, a Firefox browser will open.

3. When driver.get(URL); will execute, WebDriver will visit to the specified web application URL in your current web browser.

4. WebElement findElement(By by) will receive search parameters (XPath) and finds the first matching element present on the current web page and store it in a variable email whose type is WebElement.

5. email.sendKeys(); will send your email id in the mailbox. Similarly will happen for the password.

6. Now click() method will click on the login button. 
7. After login successfully, the close() method will close the current window and exits web browser.

Final words
Hope that this tutorial has covered all important points related to find element by XPath in Selenium with practical example and explanation. I hope that you will have understood this concept nicely and automated test script in your Eclipse IDE.
Thanks for reading!