xpath

Parent
The parent axis contains only a maximum of one node. The parent node may be either the root node or an element node.the parent axis contains one node.
1) //*[@class='zOB73']/parent::*
Result à it will selects parent of the @class='zOB73' node
clip_image002

1) //*[@class='zOB73']/parent::div[1]àit will select first div node
2) //*[@class='zOB73']/parent::div à it will select all div nodes

Following
Following axis contains all nodes in the same document as the context node that are after the context node in document order.
Ex:1
//*[@class='zOB73']/following::div
Result à it will selects only div nodes after the current node
clip_image004

Ex2://*[@class='s2h6df']/following::*
Result -à it will selects all element nodes after the current node
clip_image006

Following Sibling
The following-sibling axis selects those nodes that are siblings of the context node (that
is, the context node and its sibling nodes share a parent node) and which occur later in
document order than the context node.

Ex:1//*[@class='gb_T']/following-sibling::div
clip_image008

Ex:1//*[@class='gb_T']/following-sibling::*
clip_image010

Ancestor
Selects all ancestors (parent, grandparent, etc.) of the current node
1) //*[@id='cst']/ancestor::div[@id='viewport']
clip_image012

2) //*[@id='searchform']/ancestor::div
clip_image014

3) //*[@id='cst']/ancestor::*
clip_image016

4) //*[@id='logocont']/ancestor::div[position()=3]
clip_image018


5) //*[@id='logocont']/ancestor-or-self::div
Ancestor-or-self lets you select any ancestors [e.g., Parent and Grandparent] of the current node including the current node.
clip_image020

Descendant
Selects all descendants (children, grandchildren, etc.) of the current node
1) //*[@id='viewport']/descendant::div
Result à it will display all div nodes from current node

clip_image022

1) //*[@class='s2h6df']/descendant::div[@class='HKjQUb']
Result à it will display specific node from current node

clip_image024

2) //*[@id='viewport']/descendant-or-self::div
Descendant-or-self lets you select all descendants [e.g., Children and Grandchildren] of the current node including the current node.
clip_image026

3) //*[@id='viewport']/descendant-or-self::div[3]
Resultà it will select 3rd position from then current node
clip_image028

Child
Selects all class nodes that are children of the current nodebecause below example we are selected class node
//*[@id='viewport']/child::div
Result à it will display all child nodes of current node specific to div node only

Note: other than div is there It will not select
clip_image030

//*[@id='viewport']/child::div[@id='gac_scont']
Result à it will display specific node from current node
clip_image032

Child :: *
//*[@id='viewport']/child::*
Result àSelects all element children of the current node
clip_image034

Child::Text()
Selects all text node children of the context node
Ex: //a/child::text()
clip_image036


Child::node()
Selects all children of the context node, whatever their node type
Note : it is very similar to child::*
Ex:1//*[@id='viewport']/child::*
clip_image034[1]

Ex 2: //*[@id='viewport']/child::div[position()=2]
Result à it will select child node of div which is on 2nd position
clip_image038

Preceding
The preceding axis contains all nodes in the same document as the context node that are before the context node in document order.
Ex:
//*[@id='viewport']/preceding::*
Result à it will select all element nodes before the current node
clip_image040

Ex 2:
//*[@id='main']/preceding::div
It will select only div nodes before the current node
clip_image042

Preceding – position
//*[@id='main']/preceding::div[position()=1]
Result: it will select node which Is on position 1 of div
clip_image043

Preceding –sibling
The preceding-sibling axis selects those nodes which are siblings of the context node (that is, the context node and its sibling nodes share a parent node) and which occur earlier in document order than the context node.

Ex1: //*[@id='main']/preceding-sibling::*
RESULT -à it will selects all element nodes before the current node of same level
clip_image045

Ex2: //*[@id='main']/preceding-sibling::div
RESULT -à it will selects all div nodes before the current node of same level
clip_image046

Ex:3: //*[@id='main']/preceding-sibling::div[position()>2]
Result à it will select all div nodes before the current node which is 2ndposition on wards
clip_image048

Attribute
Selects the attribute of the context node
Ex:1: //*[@id='viewport']/attribute::*
Selects the all attributes of the context node
clip_image049

Ex:2//*[@id='viewport']/attribute::class
Selects the class attribute of the context node
clip_image050

Ex:3
//div/attribute::class
Selects the class attribute of the context node
clip_image052

Ex: //div/a[text()="Gmail"]
Result àThis will find a link that has text ="Gmail"
clip_image054

Self
//*[@id='viewport']/self::*
Result àSelf returns the current node.
clip_image056

Few more examples:
Ex: //*[@id='viewport']/div[count(*)>0]àResult it will select all div node of the current node
clip_image058

Ex: //*[@id='viewport']/div[last()-3]
/*[last ()-3] describes the 4th upper node (div [2]) from last node.

Ex //div[@class='s2h6df']/child::div[position()=1]
Selects the first child of the context node

clip_image060

Ex: //div[@class='s2h6df']/child::div[position()=last()]
Selects the first child of the context node
clip_image062

Ex: //div[@class='s2h6df']/child::div[position()=last()-1]
Selects the last but one child of the context node
clip_image064

Ex: //div[@class='s2h6df']/child::div[position()>1]
Selects the all child node except first context node
clip_image066

Parent
The parent axis contains only a maximum of one node. The parent node may be either the root node or an element node.the parent axis contains one node.
1) //*[@class='zOB73']/parent::*
Result à it will selects parent of the @class='zOB73' node
clip_image002
1) //*[@class='zOB73']/parent::div[1]àit will select first div node
2) //*[@class='zOB73']/parent::div à it will select all div nodes

Following
Following axis contains all nodes in the same document as the context node that are after the context node in document order.
Ex:1
//*[@class='zOB73']/following::div
Result à it will selects only div nodes after the current node
clip_image004

Ex2://*[@class='s2h6df']/following::*
Result -à it will selects all element nodes after the current node
clip_image006

Following Sibling
The following-sibling axis selects those nodes that are siblings of the context node (that
is, the context node and its sibling nodes share a parent node) and which occur later in
document order than the context node.
Ex:1//*[@class='gb_T']/following-sibling::div
clip_image008

Ex:1//*[@class='gb_T']/following-sibling::*
clip_image010

Ancestor
Selects all ancestors (parent, grandparent, etc.) of the current node
1) //*[@id='cst']/ancestor::div[@id='viewport']
clip_image012

2) //*[@id='searchform']/ancestor::div
clip_image014

3) //*[@id='cst']/ancestor::*
clip_image016

4) //*[@id='logocont']/ancestor::div[position()=3]
clip_image018

5) //*[@id='logocont']/ancestor-or-self::div
Ancestor-or-self lets you select any ancestors [e.g., Parent and Grandparent] of the current node including the current node.
clip_image020

Descendant
Selects all descendants (children, grandchildren, etc.) of the current node
1) //*[@id='viewport']/descendant::div
Result à it will display all div nodes from current node
clip_image022

1) //*[@class='s2h6df']/descendant::div[@class='HKjQUb']
Result à it will display specific node from current node
clip_image024

2) //*[@id='viewport']/descendant-or-self::div
Descendant-or-self lets you select all descendants [e.g., Children and Grandchildren] of the current node including the current node.
clip_image026

3) //*[@id='viewport']/descendant-or-self::div[3]
Resultà it will select 3rd position from then current node
clip_image028

Child
Selects all class nodes that are children of the current nodebecause below example we are selected class node
//*[@id='viewport']/child::div
Result à it will display all child nodes of current node specific to div node only
Note: other than div is there It will not select
clip_image030

//*[@id='viewport']/child::div[@id='gac_scont']
Result à it will display specific node from current node
clip_image032

Child :: *
//*[@id='viewport']/child::*
Result àSelects all element children of the current node
clip_image034

Child::Text()
Selects all text node children of the context node
Ex: //a/child::text()
clip_image036

Child::node()
Selects all children of the context node, whatever their node type
Note : it is very similar to child::*
Ex:1//*[@id='viewport']/child::*
clip_image034[1]

Ex 2: //*[@id='viewport']/child::div[position()=2]
Result à it will select child node of div which is on 2nd position
clip_image038

Preceding
The preceding axis contains all nodes in the same document as the context node that are before the context node in document order.

Ex:
//*[@id='viewport']/preceding::*
Result à it will select all element nodes before the current node
clip_image040

Ex 2:
//*[@id='main']/preceding::div
It will select only div nodes before the current node
clip_image042

Preceding – position
//*[@id='main']/preceding::div[position()=1]
Result: it will select node which Is on position 1 of div
clip_image043

Preceding –sibling
The preceding-sibling axis selects those nodes which are siblings of the context node (that is, the context node and its sibling nodes share a parent node) and which occur earlier in document order than the context node.
Ex1: //*[@id='main']/preceding-sibling::*
RESULT -à it will selects all element nodes before the current node of same level
clip_image045

Ex2: //*[@id='main']/preceding-sibling::div
RESULT -à it will selects all div nodes before the current node of same level
clip_image046

Ex:3: //*[@id='main']/preceding-sibling::div[position()>2]
Result à it will select all div nodes before the current node which is 2ndposition on wards
clip_image048

Attribute
Selects the attribute of the context node
Ex:1: //*[@id='viewport']/attribute::*
Selects the all attributes of the context node
clip_image049

Ex:2//*[@id='viewport']/attribute::class
Selects the class attribute of the context node
clip_image050


Ex:3
//div/attribute::class
Selects the class attribute of the context node
clip_image052

Ex: //div/a[text()="Gmail"]
Result àThis will find a link that has text ="Gmail"
clip_image054

Self
//*[@id='viewport']/self::*
Result àSelf returns the current node.
clip_image056

Few more examples:
Ex: //*[@id='viewport']/div[count(*)>0]àResult it will select all div node of the current node
clip_image058

Ex: //*[@id='viewport']/div[last()-3]
/*[last ()-3] describes the 4th upper node (div [2]) from last node.

Ex //div[@class='s2h6df']/child::div[position()=1]
Selects the first child of the context node

clip_image060

Ex: //div[@class='s2h6df']/child::div[position()=last()]
Selects the first child of the context node
clip_image062

Ex: //div[@class='s2h6df']/child::div[position()=last()-1]
Selects the last but one child of the context node
clip_image064

Ex: //div[@class='s2h6df']/child::div[position()>1]
Selects the all child node except first context node
clip_image066

Comments