0% found this document useful (0 votes)
6 views7 pages

xpath_2

xp

Uploaded by

Aravind P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

xpath_2

xp

Uploaded by

Aravind P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

XPath Sibling

By: N Nidhish Karkera


Emp.id- 781552
Xpath Sibling is defined as a function for retrieving a web element. A Sibling in
Selenium Webdriver is a function used to fetch a web element which are sibling with
same parent element or the element on the same level. If the parent element is
known, then the web element can be easily found or located that can use the sibling
attribute of the Xpath expression in selenium webdriver.
Following and preceding siblings are just a few of the Axis that can be used in
Selenium to discover elements on a web page using XPath. All siblings are referred to
be the parent node’s children. So, we can use the following sibling if we’re referring
to one of the children and want to browse to other children from the same parent
who follow it.
When to use Xpath Sibling?
• There are some situations when regular XPath cannot be used to find an element. In
such situation, we need different functions from the xpath query.
• There some important XPath functions like XPath contains, parent, ancestors, following-
sibling, etc.
• With the help of these functions, you can create complex XPath expressions.
• If the parent element is known, then the web element can be easily found or located
that can use the sibling attribute of the Xpath expression in selenium web driver.

There are two types of Xpath Siblings:


1. Following-sibling
2. Preceding-sibling

1. following-sibling
The Xpath sibling axis is similar to the axis that comes after it. Only sibling nodes, i.e.
nodes on the same level as the current node, are recognised by following-sibling.
We can make use of this when the class names are same .
2. preceding-sibling
What is preceding sibling in XPath?
The preceding-sibling axis indicates all the nodes that have the same parent as the
context node and appear before the context node in the source document.

Syntax of XPath Sibling- Following-sibling:

//node [attribute='value of attribute']//following-

sibling:attribute

Example 1: Consider there are two fields, first name and last name. To locate the

lastname field, we can use the following xpath:

//select[@id='fname']/following-sibling::select

Example 2: https://www.educba.com/ site


Using following-sibling, we can locate the “login” by using id of “Certification
Course”. Xpath:- //li[@id='mega-menu-item-138330']//following-sibling::li

Example 3: Since the class names of Job, Organizations, Qualifications, Nationalities


are same, it is difficult to locate particular element using simple xpath, In this case
we can locate the required web Element, by using either parent class name or the
first elements class name.

Xpath- //ul[@id='mega-menu-primary']//following-sibling::li

Here 'mega-menu-primary' is id of parent, and following-sibling::li will point to the

child.
Example 4: Orange HRM site
Here we can locate “Qualification” using the class name of “job” .

Xpath: //li[@class='oxd-topbar-body-nav-tab --parent']//following-


sibling::li//following-sibling::li
Syntax of XPath Sibling- Preceding-sibling:

//node [attribute='value of attribute']//preceding-

sibling:attribute
Example 1:
Xpath- //li[@id='mega-menu-item-138328']//preceding-sibling::li
The Above Xpath will locate the first preceding or the first web element in the
current level.

Here, @id='mega-menu-item-138330’ is the id of “Certification Courses” and the


keyword, “preceding-sibling::li” will locate the search bar, which is the first preceding
web element in that list.

Example 2- OrangeHRM site: Just like following-sibling, To locate “User


Management” we can use class name of “job” as follows,

Xpath- //li[@class='oxd-topbar-body-nav-tab --parent']//preceding-sibling::li

You might also like