You are on page 1of 7

XPaths Axes:

1. Child axes:
i. The child axis is one of the 13 XPath axes that contains all the child
nodes of the current context. It selects all children elements of the
current node.

ii.XPath of all children elements: //span[@class =


'worldwide__list']//child::a (1 of 6 matched)

2. Parent Axes:
i. The parent axis selects the parent of the current node. The parent
node may be either root node or element node. The root node has
no parent.
ii. XPath(Parent node): //input[@id = 'text']//parent::span (1 of 1
matched)

iii. XPath(Parent node): //input[@id = 'text']//parent::* (1 of 1 matched)

3. Ancestor Axis:
i. The ancestor axis selects all ancestor elements (parent, grandparent,
great-grandparents, etc.) of the current node. This axis always
contains the root node (unless the current node is the root node).

ii. XPath(Parent node): //input[@id = 'u_0_a']//ancestor::label


iii. XPath(Grandparent node): //input[@id = 'u_0_a']//ancestor::td

4. Descendant Axis:
i. The descendant axis selects all descendant elements (children,
grandchildren, etc) of the current node. Let’s take an example to
understand the concepts of the descendant axis.

ii. XPath: //div[@class = 'signup_form new']//descendant::input (1 of 3)

iii. XPath(Username): //div[@class='signup_form


new']//descendant::input[1]

5. Following-sibling:
i. We can use the concept of following-sibling in xpath for identifying
elements in Selenium. It identifies the siblings of the context node.
The siblings should be located at the equal level of the existing node
and should have the same parent.
ii. Let us see an example of an element with ul tag having more than
one child with li tag. Then let us try to locate the fifth li element
(Effective Resume Writing) from the first li element with class
attribute sreading

iii. //li[@class='sreading']/following-sibling::li[4]

6. Following:
i. following: This function will return the immediate element of the
particular component.

ii. Here our XPath query will be like

iii. Xpath=//*[@type='text']//following::input

7. Preceding-sibling:
i. 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.
ii.
XPath: //a[text() = 'Videos']//preceding-sibling::a (1 of 3)

8. Preceding:
i. This indicates all the nodes that appear before the context node in the HTML
DOM structure. This does NOT indicate descendent, attribute, and
namespace.
ii. XPath: //input[@id = 'u_0_2']//preceding::input (1 of 4 matched).

Ways of writing Dynamic xpath in selenium:

1. Using Single Slash


2. Using Double Slash
3. Using Single Attribute
4. Using Multiple Attribute
5. Using AND
6. Using OR
7. Using contains()
8. Using starts-with()
9. Using text()
10.Using last()
11.Using position()
12.Using index
13.Using following XPath axes
14.Using preceding XPath axes

You might also like