How to select checkbox using position or last function in Xpath

Posted By : Anuj Gupta | 04-Sep-2018

Today, we will discuss about selecting a checkbox using position() or last() functions. Well, selecting a checkbox is not a difficult task if a checkbox has an id, name, and class or a combination of these attributes.

If these attributes are not present then we will not be able to locate checkbox or radio buttons. So, let's discuss in a brief:

1) Using Position() function in Xpath:

If you want to select a checkbox on the basis of its position using Xpath then we can use position() function in Xpath.

Let's say, if you want to select a second and fourth position then we can use below mentioned command:

                   xpath=(//input[@type='checkbox']) [position()=2];
                   xpath=(//input[@type='checkbox']) [position()=4];
        

2) Using Last() function in Xpath:

You can use last() function to select the last checkbox among the similar objects.

                  xpath=(//input[@type='checkbox'])[last()]  
        

If you want to select a second last checkbox then you can also use the below mentioned command:

                  xpath=(//input[@type='checkbox'])[last()-1]
        

So, here is a small example to make it clearer.

               package Selenium; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 

import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.chrome.ChromeDriver; 

import org.testng.annotations.BeforeTest; 

import org.testng.annotations.Test; 

  

public class TestNG { 

    public WebDriver driver; 

     @BeforeTest 

        public void setup() throws Exception {   

      System.setProperty("webdriver.chrome.driver", "/home/anuj/chromedriver"); 

      driver = new ChromeDriver(); 

      driver.manage().window().maximize(); 

      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 

      driver.get("http://only-testing-blog.blogspot.in/2014/09/temp.html"); 

   }     

   @Test 

     public void selectCheck(){ 

          //To select C++ checkbox using position() function. 

          driver.findElement(By.xpath("(//input[@type='checkbox'])[position()=2]")).click(); 

           

          //To select PHP checkbox using last() function. 

          driver.findElement(By.xpath("(//input[@type='checkbox'])[last()-1]")).click(); 

           

          //To select Ruby checkbox using last() function. 

         driver.findElement(By.xpath("(//input[@type='checkbox'])[last()]")).click(); 

         } 

        }  
        

                     

The first image shows the five checkbox fields and on the basis above program, C++, PHP and Ruby checkbox got selected which will show as an output in the second image.

You can also use the same thing for any element to locate it from the list of similar items.

 

If you enjoyed this blog post, share it with a friend!

 

Related Tags

About Author

Author Image
Anuj Gupta

Anuj is a passionate QA engineer and an optimistic person. He is fond of playing Chess and Basketball. He always comes up with the new ideas and is a good team player.

Request for Proposal

Name is required

Comment is required

Sending message..