XPath in Selenium Video Tutorial

You are here:

Automation engineers, Chris Lawson and Ryan Eisma, explore effective strategies for making your XPath in Selenium less brittle."Sometimes you have locator strategies that don't work the way you want it to. You can't always identify everything by name, in these cases we often find XPath, as a locator strategy, to be useful." - Automation expert, Chris Lawson

There's much debate on whether or not you should use XPath or other locator strategies. This video demo addresses some of the techniques Zenergy often utilize to make your XPath less brittle.Follow times (seconds) throughout the demo to match the audio with the text below.

  • Time (00:50): For this demo Ryan Eisma will demonstrate how to utilize XPath using the Zenergy Technologies website. When retrieving the XPath from the browser, when inspecting the element, you get an absolute path that starts at the top of the page and narrows down all the way to the element. The problem is that you are dealing with the hierarchy structure, which may not always be the same. For instance, we could add an element to the page or move where the element is located, but doing so would cause the path to change and ultimately lead to test failures.
  • Time (01:19): First, we are going to copy the tag for the element; we have an anchor tag, followed by a couple selectors as well as the text that is currently displayed on the page. If we were trying to get the element with Selenium through XPath we want to:
    • Ensure that the selector is as unique as possible. In this case, the most unique aspect of this selector would be the title attribute element.
    • In order to get a relative path to that element you will want to utilize a "wild card". (See demo for how to reach the relative XPath, appropriately).
  • Time (01:49 - 2:10): Keep in mind that the title element might not be the same; for instance, you might have more title elements that have "Selenium automation" in them. One thing you could do in this scenario is to pair two selectors together.
  • Time (02:30): In this example, we don't have anything other than the reference of the web page the path is navigating to, however, we do have text on the page which contains the word "selenium" in it. XPath provides us with numerous functions in which it can call to just a portion of that text. We can use a "starts with" and "ends with" or in this example (02:56) we can use a "contains" along with the text function and include the part of the text that we are trying to retrieve.
  • Time: XPath also provides a sibling, a descendent, an ancestor and numerous other functions that rely to particular relationships on the page. If we were to use our same title attribute here but wanted to get to something that's below it in the hierarchy of the page we would want to use the descendent as our tag item to help us navigate to that element. The descendent tag would be followed by another attribute (in this case we used a class attribute) and then the name of the class we are wanting to navigate to. Pairing two selectors together allows you to get a unique locator for your elements. We hope these locator strategies are helpful in making your XPath less brittle and more effective.

>> Subscribe to Zenergy on Youtube for more videosRead our blog, Five Common Missteps in Selenium Test Automation Implementations.