XPath is a query language for selecting nodes from an XML document. In Selenium, we can use XPath to locate a WebElement in the DOM (Document Object Model) to interact and perform any operation on the page.
//tagname[@attribute=’Value‘]
While identifying a WebElement using XPath, we have to ensure that it’s unique and reliable enough to increase the test script stability. This can be achieved using a combination of tagname
and attribute
.
The below practices can help to write an effective XPath:
//
).id
or name
attribute.class
name or combination of class
names.Sibling
, Ancestors
, or Parents
to identify it uniquely.text
comparison to locate since any change in the text will break the XPath./
).