Types of XPath

  1. Absolute XPath

Absolute XPaths starts with the root of the HTML pages.

Absolute XPaths are not advisable for most of the time due to following reasons

  1. Absolute XPaths are lengthier and hence they are not readable
  2. Absolute XPaths are brittle when minor structural changes are done to the web pages

Absolute XPaths shall be used only when a relative XPath cannot be constructed. (highly unlikely). Absolute XPaths tends to break as web pages/content is changed. Hence it is not recommended to use absolute XPath in Selenium.

Syntax: Absolute XPaths start with  /html

Example: /html/body/div[1]/div/div[2]/form/div[2]/input

2. Relative XPath

Relative XPaths is used for locating an element with respect to an element known as XPath. The element of your choice is referred relative to a known element.

Syntax: Relative XPaths are started with two forward slashes ‘//’.

Example: //div[@id=’divUsername’]/input

Note: Absolute XPaths are faster than the relative XPaths

3. Exact XPath

Locating elements using their attributes, values and inner text of the elements.

Why do we need to master many XPath syntaxes?

We may not be able to locate the elements directly using their unique attributes as some elements do not have unique attributes (id or name). Some attributes are dynamically changed. There could be elements without attributes. Hence we may have to locate them differently than the static elements.

XPath can be used for:

  • Locating elements with respect to a known element
  • Locating elements with partially static attribute values
  • Locating elements without attributes or without unique attributes

XPath can do bidirectional navigation (Going forward and backward)

XPath is one of most flexible and strongest location strategy and can be used for locating an element in a web page.

Using XPath for locating elements in HTML

We have our own way of introducing (explaining) XPath to the trainees in our public training programs.

When A=B and B=C. We ask the students what can be derived from these expressions. Immediately the students reply with A=C, even before the question is asked.

Then we give following two statements

  • XPath is for  locating the elements or nodes  in XML documents
  • XML and HTML has similar syntax (HTML is a subset of  XML)

Hence XPath can be used to locate elements in HTML pages (web pages).

Mastering in XPath for Selenium Test Automation

Introduction

Selenium test automation engineers must be comfortable in locating elements in web pages effectively. XPath are very powerful location strategies used in Selenium as compared to other location strategies (id, name, linkText, partialLinkText and tagName )

Mastering XPath is essential for the Selenium test automation engineers to locate dynamic web elements and elements without id or name. Tagname is not a useful location strategy to locate elements as there are many elements with the same tagname. LinkText is only useful for the <a> tags only. Also, it is not useful when the visible text changes as in multilingual systems.

It is noted that most of the amateur Selenium automation engineers do not pay much attention to master location strategies. Recording and playback will not work with the applications with dynamic content. This leads to failure of test automation scripts (brittle) when web pages with dynamic contents are automated. Most of the testers rely on extracting the XPaths from browser plugins. These tools have got limitations and do not provide best values for dynamic elements. We will discuss XPath in detail with examples and explore few tools to extract XPaths automatically.