XPath for Selenium IDE
XPath for Selenium IDE
An introduction to the advantages of using XPath with your Selenium web testing scripts.
Join the DZone community and get the full member experience.Join For Free
Download the blueprint that can take a company of any maturity level all the way up to enterprise-scale continuous delivery using a combination of Automic Release Automation, Automic’s 20+ years of business automation experience, and the proven tools and practices the company is already leveraging.
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.
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:
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//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:
Find the cell with value “configuration” between the rows.
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.
Opinions expressed by DZone contributors are their own.