Xpath Function Using Preceding
Xpaths Preceding :-
This method helps in locating element before the current node, as in the preceding element from the current node with XPath. In the below-mentioned example, the first one will locate element with the field as email whereas the other one will locate all elements before the current node i.e. password field. This is also useful in locating elements that cannot be located by any means and can be traversed through. For example, in the case of cross browser testing, sometimes few elements cannot be recognized on IE browsers, legacy browser versions. In this case traversing to these elements using precedence or following could be helpful.
Example 1: Xpath=//input[@name='password']//preceding::input[1]
Example 2: Xpath=//input[@name='password']//preceding::input
Xpaths Preceding-Sibling :-
This is a concept similar to following sibling, the only difference in functionality is that of preceding. So, in this case, you can switch among the siblings, but in this, you will switch from the context node been a child and move to the preceding node, you wish to locate. Both the child will share the same parent. Using the same example as mentioned in the following sibling, we will now move from the sign-up link to the login link using preceding-sibling. The syntax for the same is:-
Example 1: Xpath= //li[@class='login']//preceding-sibling::li[1]