HomeSelenium

Selenium Locators – last() XPath Function

Selenium Locators – last() XPath Function

<<Previous Post                            << Complete Tutorial>>                              Next Post>

In the previous article, I have practically demonstrated starts-with() XPath Function.

In this article, I am going to explain the next XPath Function known as last()

If there are n number of child tags under a parent tag, then to locate the last child tag, we can use last() XPath Function.

Let’s get started.

Selenium Locators – last() XPath Function

Follow the below steps for practicing the last() XPath Function:

1) Open http://compendiumdev.co.uk/selenium/basic_web_page.html in Chrome Browser, where ChroPath is already installed (Refer ChroPath installation in Chrome Browser here and its usage in Chrome Browser here ) as shown below:

last() XPath Function - Website Launch

2) Open ChroPath in Chrome Browser where ‘Rel XPath’ is selected as shown below:

last() XPath Function - Rel XPath

 

3) Execute the Relative XPath Expression //p into the ‘ChroPath’ and observe that the two paragraphs are getting located as shown below:

last() XPath Function - All p s located

4) Let us locate the first paragraph p tag using the Relative XPath Expression //p[1] by executing in the ChroPath as shown below:

last() XPath Function - First Para Located

5) Let’s find the last paragraph p tag using last() XPath Function in Relative XPath Expression.

In place of the above [1] in the above step, we will use the last()  XPath Function as shown below:

//p[last()]

Execute the above Relative XPath Expression in the ‘ChroPath’ and observe that the last paragraph out of two paragraphs got located as shown below:

last() XPath Function - Last Para Located

6) As we have now understood the usage of last() XPath Function in locating the last child tags.

Now, let’s play more with this last() XPath Function to achieve different things.

Let’s start this by locating the last but one paragraph p tag using the last() XPath Function by creating a Relative XPath Expression //p[last()-1]

Execute the above Relative XPath Expression in the ‘ChroPath’ and observe that the last but one paragraph out of two paragraphs got located as shown below:

last() XPath Function - last but one p tag

7) Let’s find the first child tag of ‘body’ tag in this page by using the Relative XPath Expression //body/*[1]

Execute the above Relative XPath Expression in ChroPath and observe that the first child tag of the body tag got located as shown below:

last() XPath Function - body first child

8) Let’s find the last child tag of ‘body’ tag in this page by using the Relative XPath Expression //body/*[last()]

Execute the above Relative XPath Expression in ChroPath and observe that the last child tag of the body tag got located as shown below:

last() XPath Function - body last child

9) Open http://omayo.blogspot.com/ in Chrome Browser, where ChroPath is already installed (Refer ChroPath installation in Chrome Browser here and its usage in Chrome Browser here ) as shown below:

Relative XPath - More complex application

10) Let’s find the first input tag in the above page using Relative XPath Expression (//input)[1]

Execute the above Relative XPath Expression in ChroPath and observe that the first input tag got located as shown below:

last() XPath Function - input first

11) Let’s find the last input tag in the above page using Relative XPath Expression (//input)[last()]

Execute the above Relative XPath Expression in ChroPath and observe that the last input tag got located as shown below:

last() XPath Function - input last

12) Let’s find the last but one input tag in the above page using Relative XPath Expression (//input)[last()-1]

Execute the above Relative XPath Expression in ChroPath and observe that the last but one input tag got located as shown below:

last() XPath Function - last but one

13) Let’s find the last but two input tag in the above page using Relative XPath Expression (//input)[last()-2]

Execute the above Relative XPath Expression in ChroPath and observe that the last but two input tag got located as shown below:

last() XPath Function - last but two

14) Open http://compendiumdev.co.uk/selenium/basic_web_page.html in Chrome Browser, where ChroPath is already installed (Refer ChroPath installation in Chrome Browser here and its usage in Chrome Browser here ) as shown below:

last() XPath Function - Website Launch

15) Open ChroPath in Chrome Browser where ‘Rel XPath’ is selected as shown below:

last() XPath Function - Rel XPath

16) Let’s locate the first p tag in the page having the class attribute value as ‘main’ as shown below:

Relative XPath Expression for locating the first p tag in the page having the class attribute value as ‘main’ is //p[1][@class=’main’]

Execute the above Relative XPath Expression in ChroPath and observe that the first paragraph having the class attribute value as ‘main’ got located as shown below:

last() XPath Function - main first

17) Let’s locate the last p tag in the page having the class attribute value as ‘sub’ as shown below:

Relative XPath Expression for locating the last p tag in the page having the class attribute value as ‘sub’ is //p[last()][@class=’sub’]

Execute the above Relative XPath Expression in ChroPath and observe that the last paragraph having the class attribute value as ‘sub’ got located as shown below:

last() XPath Function - sub last

Here concludes this article.

In the next article, I will practically demonstrate the position() XPath function.

Next Steps:

  • > To learn more about Selenium, continue to the next post (Click on Next Post link below)
  • > Check complete Selenium Tutorial Contents here (Click here)

Please leave your questions/comments/feedback below.

Happy Learning 🙂

About Me > Arun Motoori

On a mission to contribute to the Software Testing Community in all possible ways.

<<Previous Post                            << Complete Tutorial>>                              Next Post>

If you like this article, share it on LinkedIn using the below button:

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *