XPath: Using Ancestors
XPath Ancestor:
The ancestor axis is used to select the parent, grandparent, or any higher-level ancestor of the current node. This is helpful when you need to navigate upwards in the document tree from a child element to its ancestors.
In the examples below, the first XPath expression selects the immediate parent (div) of the email input field, while the second one selects its grandparent, which is the form element.
This selects the parent div element of the input field with the name "email."
Example 1: XPath= //input[@name='email']//ancestor::div[1]
This selects the form element that is the grandparent of the input field with the name "email."
Example 2: XPath= //input[@name='email']//ancestor::form