HomeSelenium

Selenium Locators – position() XPath Function

Selenium Locators – position() XPath Function

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

In the previous article, I have practically demonstrated last() XPath Function.

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

If there are n number of child tags under a parent tag, then to locate the child tag in a particular position among the other child tags, we can use postion() XPath Function.

Let’s get started.

Selenium Locators – postion() XPath Function

Follow the below steps for practicing the position() 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’s locate the first paragraph p tag using the position() XPath Function in Relative XPath Expression.

Relative XPath Expression: //p[position()=1]

Execute the above Relative XPath Expression in the ‘ChroPath’ and observe that the first paragraph is getting located as shown below:

position() XPath Function - first paragraph

5) Let’s locate the second paragraph p tag using the position() XPath Function in Relative XPath Expression.

Relative XPath Expression: //p[position()=2]

Execute the above Relative XPath Expression in the ‘ChroPath’ and observe that the second paragraph is getting located as shown below:

position() XPath Function - second paragraph

6) 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

7) Let’s find the first input tag in the above page using position() XPath Function:

Relative XPath Expression:  (//input)[position=1]

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

position() XPath Function - input first

8) Let’s find the eight input tag in the above page using position() XPath Function:

Relative XPath Expression:  (//input)[position=8]

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

position() XPath Function - eight paragraph

Here concludes this article.

In the next article, I will start explaining the XPath AXES Concepts.

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 *