XPath is a selenium technique that is used to navigate through the html structure of a webpage. It is a syntax or a language that helps finding elements on webpage using XML path expression.
In Selenium automation, there may be times when elements cannot be found with general locators like ID, name, class, etc. And this is when XPath is used to locate those elements on the webpage. XPath in Selenium may be used on both XML and HTML documents.
syntax of XPath
XPath in Selenium provides many essential XPath functions and axes to write effective XPaths of the web elements and define unique locators
⦁ // : to select current node
⦁ tagname : tagname of specific node
⦁ @: to select attribute
⦁ attribute: it is the attribute name of the node.
⦁ value: it is the value of the node
To find the element on web pages accurately there are different types of locators:
Type of XPath
Absolute xpath : It start with single forward-slash (/), absolute xpath refers a direct way to find an element. The major drawback of this xpath is if there any changes of element’s path the xpath will be fail
"/html/body/div[1]/div/div[2]/header/div/div[2]/a/img"
Relative Xpath : It start with double forward-slash(//), the path begins from the middle of the HTML DOM structure.
Relative Xpath : "//div[@role='alert']"
How to handle Elements in Selenium using XPath?
1) Using Attributes: select nodes or list of nodes on the basis of attributes like ID , Name, Classname, etc
“//form[@id=’loginForm’]/input[3]”
2)Contains: is a method used in XPath expression. It is used when the value of any attribute changes dynamically,
Complete value of ‘Type’ is ‘submit’ but using only partial value ‘sub’.
"Xpath=//*[contains(@type,'sub')]"
Complete value of ‘name’ is ‘btnLogin’ but using only partial value ‘btn’.
"Xpath=//*[contains(@name,'btn')]"
3)Using Text: search for an element using the text that it contains too. For instance, to select a link that says “Click Me”
"//a[text()='Click Me']"
4)Using or & and: in or expression two conditions are used whether 1 condition or 2condition can be true.
Xpath=//*[@type='submit' or @name='btnReset']
In AND expression, two conditions are used, both conditions should be true to find the element. It fails to find element if any one condition is false.
"Xpath=//input[@type='submit' and @name='btnLogin']"
What are XPath Axes?
An XPath axes define the node-set relative to the current (context) node. It is used to locate the node that is relative to the node on that tree.
All the XML DOM elements are in a hierarchical structure and can be either located using Absolute paths or Relative paths. For this, XPath provides specific attributes called “XPath Axes”.
Different XPath Axes Used In Selenium Testing
ancestor: These axes indicate all the ancestors relative to the context node, also reaching up to the root node.
ancestor-or-self: This one indicates the context node and all the ancestors relative to the context node, and includes the root node.
attribute: This indicates the attributes of the context node. It can be represented with the “@” symbol.
child: This indicates the children of the context node.
descendent: This indicates the children, grandchildren, and their children (if any) of the context node. This does NOT indicate the Attribute and Namespace.
descendent-or-self: This indicates the context node and the children, and grandchildren and their children (if any) of the context node. This does NOT indicate the attribute and namespace.
following: This indicates all the nodes that appear after the context node in the HTML DOM structure. This does NOT indicate descendent, attribute, and namespace.
following-sibling: This one indicates all the sibling nodes (same parent as the context node) that appear after the context node in the HTML DOM structure. This does NOT indicate descendent, attribute, and namespace.
namespace: This indicates all the namespace nodes of the context node.
parent: This indicates the parent of the context node.
preceding: This indicates all the nodes that appear before the context node in the HTML DOM structure. This does NOT indicate descendent, attribute, and namespace.
preceding-sibling: This one indicates all the sibling nodes (same parent as context node) that appear before the context node in the HTML DOM structure. This does NOT indicate descendent, attribute, and namespace.
self: This one indicates the context node.
Benefits of using XPath:
1.Powerful Element Selection: XPath provides a precise and flexible way to locate elements within an XML or HTML document. With XPath expressions, you can target elements based on their attributes, text content, relationships with other elements, and more.
2.Cross-Platform Compatibility: XPath is supported by various programming languages and tools, making it highly portable across different environments. Whether you’re using Python, Java, JavaScript, or any other language, you can use XPath to navigate and extract data from XML and HTML documents.
3.Ease of Use: XPath syntax is relatively straightforward and intuitive, especially for those familiar with XML or HTML. Its simple yet powerful syntax allows developers and testers to write expressive queries for element selection and extraction.
4.Dynamic Element Handling: XPath offers functions like contains(), starts-with(), and ends-with() that enable dynamic element handling. This is particularly useful when dealing with web pages where element attributes or text content may change dynamically.
5.Reduced Maintenance Overhead: XPath expressions can help reduce maintenance overhead by providing a stable and reliable way to locate elements, even when the document structure changes. By using XPath selectors intelligently, you can create robust and maintainable automation scripts.
6.XPath Axes: XPath provides various axes, such as ancestor, descendant, parent, and sibling axes, which allow for more complex element navigation. These axes enable traversal through the document tree, facilitating advanced querying and selection of elements.
7.Integration with Automation Tools: XPath is commonly used in conjunction with automation tools like Selenium WebDriver for web scraping and testing purposes. Its integration with these tools enhances their capabilities and allows for more efficient and reliable automation workflows.
8.Extensibility: XPath is extensible, meaning that it can be extended with custom functions or extensions to suit specific requirements. This extensibility makes XPath a versatile tool for a wide range of applications beyond basic element selection.
Overall, XPath offers a powerful and versatile solution for navigating XML and HTML documents, selecting elements, and extracting data. Its simplicity, cross-platform compatibility, and dynamic element handling capabilities make it a preferred choice for web scraping, automation, and data extraction tasks.
Comments