XPath

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

ExampleSeleniumXPath

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());
    driver.get("http://www.javacodegeeks.com");
    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(element.getText());
    System.out.println("Page title is: " + driver.getTitle());
    driver.quit();
  }
}

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

driver.get("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.

pom.xml

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

 <groupId>SeleniumXPath</groupId>
 <artifactId>SeleniumXPath</artifactId>
 <version>1.0-SNAPSHOT</version>

 <dependencies>
   <dependency>
     <groupId>org.seleniumhq.selenium</groupId>
     <artifactId>selenium-java</artifactId>
     <version>2.45.0</version>
   </dependency>
 </dependencies>
</project>

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());
driver.get("http://www.javacodegeeks.com");

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.

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

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button