Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

XPath for Selenium IDE

DZone's Guide to

XPath for Selenium IDE

An introduction to the advantages of using XPath with your Selenium web testing scripts.

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

Simply, a Selenium Script for GUI testing can be created by using the “Record” button at the top right side of the Selenium IDE. This button records browser actions (user clicks and keyboard entries) sequentially. Easiness comes with its side effects too. When you write Selenium scripts with this method, those scripts don't produce the same outputs in every condition due to dynamic elements on the page. The recording method is very useful, but not enough to write reliable scripts.

Image title

To elevate the reliability of selenium scripts, the Xpath language is used. Xpath reduces unidentified failures and exceptions, and provides stability even if code is changed. If you just use Selenium IDE, it finds an element on a web page by its ID. Using the ID to identify an element may lead to failed test results, since IDs can be generated dynamically. Moreover, there is always a possibility that those IDs are changed by developers during the coding phase.  

XPath, the XML Path Language, is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. (https://en.wikipedia.org/wiki/XPath, 2016, 11th Aug).

As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web application. XPath extends beyond (as well as supporting) the simple methods of locating by ID or name attributes, and opens up all sorts of new possibilities such as locating the third checkbox on the page. (http://www.seleniumhq.org/docs/02_selenium_ide.jsp, 2016, 12th Aug)

As mentioned above, XPath is a language written for selecting nodes from XML documents. However, it is also useful for selecting elements in Selenium IDE. By using Xpath, exact path for an element can be defined. If an ID naming convention is just a building name, XPath is a detailed address of the building or rather latitude and longitude values of that building. For example; let’s describe the button on the 2nd row of 3rd column on the table below:

Type Description Upload Download
Delete
Configuration Startup Configuration Image title

Image title

Debug Debug Information
Script Script Language
User User Accounts

Script Block Using Record Button of IDE

<tr>
<td>type</td>
<td>id=uploadButton</td>
<td>$upload_file</td>
</tr>

Same Script Block Using Xpath

<tr>
<td>type</td>
<td>xpath=//td[./input/@value='Config']/following-sibling::td[2]//input[@type='file']</td>
<td>$upload_file</td>
</tr>

As seen from the above snippets, with XPath implementation, description is more accurate. An XPath block can be translated as:

  1. Find the cell with value “configuration” between the rows.

  2. Then find the input element 2 columns next to it (which means the 3rd column since the configuration value is on the first column).

In conclusion, usage of XPath for Selenium Scripts provides test automation systems to run with less failures and exceptions. Dynamic elements can be found easily with exact path definition, and maintenance of scripts is easier.

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
xpath ,test automation ,selenium ,test ,gui testing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}