A blog on Test Automation, Technical QA, DevOps, Testing Tools & Techniques and more. This is a personal blog. The content and opinions expressed in this blog are solely my own and do not express the views or opinions of my current or past employers.
XPath is a very powerful and widely used mechanism in Selenium for locating elements on a Web Page. XPath is used for finding nodes in an XML document, and it can also be used for finding HTML elements in XHTML. Selenium supports XPath along with various other locator strategies. XPath in Selenium extends beyond the simple locator methods by id or name attributes, and opens up all sorts of new possibilities for locating complex & dynamically rendered elements.
Developers do not always follow best practices or testability guidelines while building the applications. XPath comes to your help when you don’t have a suitable id or name attribute for the element you wish to find on a page. You can use XPath to either find the element in absolute terms or relative to an element that does have an id or name attribute.
Recently a team approached me for solving a locator problem in Selenium. They were testing an e-commerce web application. They had difficulty in selecting & adding an item to the shopping cart. The table listing the items was rendered dynamically using dynamic ids and complex HTML structure. The following figure shows how shopping cart looks like:
The textbox to enter the quantity and image to add item was deep under layer of div elements inside a td (table column) element. The id’s for these elements are generated dynamically and it was difficult to add an item from the test script. Here is HTML code for the quantity textbox:
As you can see in the above code, test cannot depend on the id attribute as it changes every time page is refreshed and name of the textbox is not unique.
This where XPath Axes comes to a help to resolve problem to find the element as well as develop a generic locator strategy. In simple terms, XPath Axes helps to find elements based on element’s relationship with other elements in a document. For more information, there is a very informative tutorial available on W3Schools on XPath & XPath Axes.
Coming back to the problem, first we need to find a unique way to identify a product inside the table. There are two columns which contain unique values namely Product & Article column. The Article column contains primary key from the database so it is highly recommended to use this value as basis to locate the product and will be always unique. Following XPath query will find the cell containing the specified Article using XPath functions contains() & text():
Now we need to find the table cell which contains the desired elements. Here we will use following-sibling axis and find the third cell from the current cell. Following query will return the cell containing Quantity & Add to cart image:
Following screen-shot shows the Selenium IDE commands to enter the Quantity and click on the Shopping Cart image:
These actions will be used to add multiple items in various test cases. We can turn these actions into a reusable function and remove the hardcoded Article Id used in above example. Following example shows the custom command implemented in Selenium User Extension and Selenium IDE:
While using XPath we need to consider the fact that these locators are dependent on structure of the page and the position of elements in the DOM. Any changes in the position will affect the locators and tests will result into failures.