DZone
DevOps Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > XPath for Selenium IDE

XPath for Selenium IDE

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

Iffet Kurukose user avatar by
Iffet Kurukose
·
Sep. 02, 16 · DevOps Zone · Tutorial
Like (3)
Save
Tweet
6.92K Views

Join the DZone community and get the full member experience.

Join For Free

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:

TypeDescriptionUploadDownload
Delete
ConfigurationStartup ConfigurationImage title

Image title

DebugDebug Information
ScriptScript Language
UserUser 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.

Integrated development environment XPath Database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • An Overview of 3 Java Embedded Databases
  • How to Optimize MySQL Queries for Speed and Performance
  • Enough Already With ‘Event Streaming’
  • Role of Development Team in an Agile Environment

Comments

DevOps Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo