58. last( ) - XPath Function





last( ) is one of the predefined methods of XPath Language which is used in XPath Statement to locate the last element node 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 last paragraph text  i.e. 'Another paragraph of text' using 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 last <p> tag using Relative XPath path containing last( ) 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[last( )] into the XPath field and click on 'Eval' button as shown below:



11. Observe that the last <p> tag is high lighted out of all the available <p> tags in the HTML code and also observe that the last paragraph text on the page is high lighted out of all the available paragraph texts on the page as shown below:



This is how the last( ) predefined method of XPath Language is used in XPath statements for locating the elements.

12. Suppose if you want to locate the last but one element using last( ) predefined method, you have to provide last( )-1 instead of last( ) in the XPath statement. Lets implement this by following the below steps.

13. Type Relative XPath path  //p[last( )-1] into the XPath field and click on 'Eval' button as shown below:



14. Observe that the last but one <p> tag is high lighted out of all the available <p> tags in the HTML code and also observe that the last but one paragraph text on the page is high lighted out of all the available paragraph texts on the page as shown below:



Please comment below to feedback or ask questions.

position( ) - XPath Function will be explained in the next post.




Followers

Labels