position( ) is one of the predefined methods of XPath Language which is used in XPath Statement to locate the element node at the specified position of the specified node type.
Lets Implement This:
1. Open http://compendiumdev.co.uk/selenium/basic_web_page.html
2. Observe that there are two paragraph texts on the page as shown below:
3. Lets locate the paragraph texts i.e. 'Another paragraph of text' by implementing position( ) method in XPath Statement by following the below steps.
4.. Right click on the Page and select 'View Page Source' option as shown below:
5. Ensure that HTML Source code of the page is displayed as shown below:
6. View the HTML Source code in the above step and lets locate the first <p> tag using Relative XPath path containing position( ) predefined XPath function by following the below steps.
7. Open http://compendiumdev.co.uk/selenium/basic_web_page.html in Firefox Browser
8. Click on 'FireBug' Add-On option
9. Select the 'Firepath' tab from the displayed 'FireBug' options
10. Type Relative XPath path //p[position( )=1] into the XPath field and click on 'Eval' button as shown below:
11. Observe that the <p> tag at position one is high lighted out of all the available <p> tags in the HTML code and also observe that the first position paragraph text on the page is high lighted out of all the available paragraph texts on the page as shown below:
12. Type Relative XPath path //p[position( )=2] into the XPath field to locate the <p> tag at second position and click on 'Eval' button as shown below:
13. Observe that the <p> tag at position two is high lighted out of all the available <p> tags in the HTML code and also observe that the second position paragraph text on the page is high lighted out of all the available paragraph texts on the page as shown below:
14. Suppose if we want to locate all the paragraph elements using position( ) function we've to use position( ) > 0 in the XPath Statement. Lets implement this by following the below steps.
15. Type Relative XPath path //p[position( )>0] into the XPath field and click on 'Eval' button as shown below:
16. Observe that all the <p> tags are high lighted in the HTML code and also observe that the paragraph texts in all the positions on the page are high lighted on the page as shown below:
17. Suppose if we want to locate all the paragraph elements except the paragraph element that is in the first position using position( ) function we've to use position( ) > 1 in the XPath Statement. Lets implement this by following the below steps.
18. Type Relative XPath path //p[position( )>1] into the XPath field and click on 'Eval' button as shown below:
19. Observe that all the <p> tags except the <p> tag in first position are high lighted in the HTML code and also observe that the paragraph texts in all the positions on the page except the one that is in the first position are high lighted on the page as shown below:
20. Suppose if you want to locate the elements from positions greater than 0 and less than 3 and don't want to locate the remaining positions using position( ) function. We've to use position( )>0 and position( )<3 in the XPath statement. Lets implement this by following the below steps.
21. Type Relative XPath path //p[position( )>0 and position( )<3] into the XPath field and click on 'Eval' button as shown below:
22. Observe that all the <p> tags that are in between the positions 0 and 3 are high lighted (i.e. position 1 and position 2 are high lighted) and also the paragraph texts on the page in 1 and 2 positions are high lighted as shown below: