Using XPath in Selenium Example

In this example we will learn how to use XPath in Selenium. We will start with the introduction of Selenium and XPath – what it is and how to use it, then we will see the working model of how to use XPath in Selenium tests.

Tools and technologies used in this example are Java 1.7, Maven, Intellij, Selenium 2, ChromeDriver, Chrome XPath Helper plugin.

1. Selenium

Selenium automates browsers. Primarily, it is for automating web applications for testing purposes. Selenium has the support of some of the largest browser vendors. In an era of highly interactive and responsive software processes where many organizations are using some form of Agile methodology, test automation is frequently becoming a requirement for software projects. Test automation is often the answer. Test automation means using a software tool to run repeatable tests against the application to be tested.

Selenium-WebDriver supports the following browsers along with the operating systems these browsers are compatible with.

  • Google Chrome
  • Internet Explorer 6, 7, 8, 9, 10 – 32 and 64-bit where applicable
  • Firefox: latest ESR, previous ESR, current release, one previous release
  • Safari
  • Opera
  • HtmlUnit
  • phantomjs
  • Android (with Selendroid or appium)
  • iOS (with ios-driver or appium)

2. Project Structure

Below is the project structure used in this example

Figure 1. Project Structure
Figure 1. Project Structure

3. Selenium test class


package com.javacodegeeks;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

* Example to show how to use XPath in Selenium test.
* @author JavaCodeGekks.com
public class ExampleSeleniumXPath {

  public static void main(String args[]) throws Exception {
    WebDriver driver = new RemoteWebDriver(new URL("http://localhost:9515"), DesiredCapabilities.chrome());
    WebElement element = driver.findElement(By.xpath("/html/body[@class='home blog gecko']/div[@id='wrapper']/div[@id='bottom']/div[1]/a[@class='ext-link']"));
    System.out.println("Page title is: " + driver.getTitle());

Create the new instance of the Chrome driver

WebDriver driver = new RemoteWebDriver(new URL("http://localhost:9515"), DesiredCapabilities.chrome());

Now use this driver to visit http://www.javacodegeeks.com


Now call the findElement() method on this driver instance using By.xpath().

WebElement element = driver.findElement(By.xpath("/html/body[@class='home blog gecko']/div[@id='wrapper']/div[@id='bottom']/div[1]/a[@class='ext-link']"));

4. Pom file

Below is the pom file which defines the dependency for the Selenium 2.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">



5. Chrome Driver

WebDriver is an open source tool for automated testing of webapps across many browsers. It provides capabilities for navigating to web pages, user input, JavaScript execution, and more. Chrome Driver is a standalone server which implements Web Driver’s wire protocol.

The ChromeDriver consists of three separate pieces. There is the browser itself (“chrome“), the language bindings provided by the Selenium project (“the driver”) and an executable downloaded from the Chromium project which acts as a bridge between “chrome” and the “driver”. This executable is called “chromedriver“.

To run the application we need to run the Chrome Driver. By default is runs at port 9515. You can download the Chrome Driver from Chrome Driver. For out example we downloaded the ‘chromedriver_win32.zip‘ file. Extract the zip file and run the exe.

Below is the screenshot of the Chrome Driver application.

Figure 2. Chrome Driver
Figure 2. Chrome Driver

5.1 Running ChromeDriver as a standalone process

Since the ChromeDriver implements the wire protocol, it is fully compatible with any RemoteWebDriver client. Simply start up the ChromeDriver executable (that works as a server), create a client, and away you go:

WebDriver driver = new RemoteWebDriver(new URL("http://localhost:9515"), DesiredCapabilities.chrome());

6. XPath Helper

To extract the xpath information for an element we will use XPath Helper (version – 1.0.13) plugin for chrome. This plugin gives you the ability to extract the XPath information. Add the plugin to the Chrome browser.

  1. Open a new tab and navigate to http://www.javacodegeeks.com/.
  2. Hit Ctrl-Shift-X to open the XPath Helper console
  3. Hold down Shift as you mouse over elements on the page. The query box will continuously update to show the full XPath

Below is the screenshot of the XPath Helper application.

Figure 3. XPath Helper
Figure 3. XPath Helper

7. Output

Below is the output which we get when we run the ExampleSeleniumXPath class.

Exelixis Media Ltd
Page title is: Java Programming, Learn Java Online with the Java Code Geeks | Java developers resource center - Java, Scala, Groovy, Android

8. Download the source file

This was an example of using XPath in a Selenium test.

You can download the full source code of this example here: SeleniumXPath

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Mohammad Meraj Zia

Senior Java Developer
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button