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 the outer div (the container with the class "google-sign-form").
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 div element with the class "google-sign-form" that holds the input field with the name "email."
Example 2: XPath= //input[@name='email']//ancestor::div[contains(@class,'google-sign-form')]