Basics of XPath

Learn the basics of XPath and strengthen your concepts by playing around with a simple example.

What is XPath?

XPath is a query language for selecting nodes from an XML document. In Selenium for UI test automation, we use XPath to locate a WebElement in the DOM (Document Object Model) to interact with and perform any operation on the page.

XPath syntax

Basic components of a WebElement:

  • HTML Tag
  • Attributes

General XPath syntax to identify a WebElement in the HTML Document:

//tagname[@attribute=’Value‘]

Example

Let’s take a look at the HTML webpage snippet and content below to write some basic XPath expressions.

The example HTML webpage is from the http://codetoautomate.com/xpath-demo-educative/ blog.

Get hands-on with 1200+ tech skills courses.