Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • About Us
  • Toggle search form

Learn About Less Talked & Used XPath Function – position()

Posted on 02/19/2025 By admin

XPath 1 provides a powerful function named “position()” . Before we move further, let’s get in to a scenario where we need this.

Consider below web page. We have six different tab headers which are actually country capital names. I used W3School reference to get below html design.

If I ask you to give me Xpath for Nth indexed tab header i.e. 2nd Header or 4th header. Probably you will write XPath as below :-

(//button[@class=’tablink’])[2] – For 2nd indexed

(//button[@class=’tablink’])[4] – For 4th indexed

Note:- In XPath index starts from one (1) not from zero as we see in programming languages.

Now if I ask you to:-

  1. Give me a XPath which locates tab headers from 2nd to 4th index?
  2. Give me a XPath which locates 2nd and 4th indexed tab headers together?
  3. Give me a XPath which locates tab headers which are before 5th index?
  4. Give me a XPath which locates tab header which are after 2nd index?

Now here you need to use XPath function called “function()”.

The position function returns a number equal to the context position from the expression evaluation context.

We can replace index with position as below:-

(//button[@class=’tablink’])[2] -> //button[@class=’tablink’][position()=2]

(//button[@class=’tablink’])[4] -> //button[@class=’tablink’][position()=4]

We can use conditional operator like greater than , less than , greater than and equal to, less than and equal to and not equal to etc. Even we can use logical operator like AND & OR with it.

To get all headers from 3rd index onward i.e 4th, 5th and 6th

//button[@class=’tablink’][position()>3]

To get all headers from 3rd index including i.e 3rd, 4th, 5th and 6th

//button[@class=’tablink’][position()>=3]

To get all headers before 5th index i.e 1st, 2nd, 3rd and 4th

//button[@class=’tablink’][position()

Uncategorized

Post navigation

Previous Post: Git Tutorial 3 – Install GIT On Windows – Step By Step
Next Post: Frequently Asked Java Program 10: Draw An Equilateral Triangle Using Stars Of Given Row

Related Posts

xpathYatra – Make Selenium Easy Uncategorized
Rest assured tutorial Uncategorized
Git Tutorial 1 – What is Version Control System? Uncategorized
image – Make Selenium Easy Uncategorized
Taking Screenshot Using TakesScreenshot Interface In Selenium Uncategorized
FREQUENTLY ASKED JAVA PROGRAMS 35 – Java Program to Reverse a String Using Stack Uncategorized

Recent Posts

  • How to Set Up Selenium WebDriver with Python Step by Step
  • How to Set Up Selenium WebDriver with Java from Scratch
  • What Is Selenium and Why It Is the Most Popular Test Automation Framework
  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark