Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Selenium Interview Question 7 – How to Select Last Five Checkboxes

Posted on 03/21/2025 By admin

This question was recently asked in a Selenium group on Facebook.

Generally these types of question are asked frequently in Selenium. Select last N elements or only odd elements or only even elements or second last elements etc. Answer of all questions will be originated from same point.

In this post, I will explain selecting last five checkboxes using Selenium.

Consider below webpage:-

HTML Code:-


Requirement is to select last five checkboxes. There are two ways to achieve this:-

Approach 1:- Using iteration

Locate all element and start iterating with starting index as (total count-5) and end index as (count-1).

Approach 2: Using XPath methods named last() and position()

Write xpath to locate check boxes whose position is greater than 5th i.e. 6th, th, 8th, 9th and 10th. We can use position() and last() methods for this.

List lastFiveCheckboxes= driver.findElements(By.xpath(“//input[position() > last()-5]”));

last() will give last index i.e. 10 and position()>(10-5) will give checkboxes from 6th position.

Java Program:-

package ScenariosPractice; import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; public class CheckLastNCheckboxes { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("file:///C:/Users/amodm/Desktop/Checkboxes.html"); // Approach 1- Using for loop List allCheckboxes = driver.findElements(By.xpath("//input[@type='checkbox']")); System.out.println("Total checkboxes found: " + allCheckboxes.size()); // Starting index will be count of elements-5. i.e. if there are 10 check boxes, // you need to go (10-5)=5th index // to check 6th element. for (int i = (allCheckboxes.size() - 5); i < allCheckboxes.size(); i++) { allCheckboxes.get(i).click(); // Putting sleep to show execution Thread.sleep(2000); } // Approach 2 - Using advanced xpath driver.navigate().refresh(); // This xpath will locate all check boxes whose position is greater than 5 i.e. // 6th, 7th, 8th, 9th and 10th. List lastFiveCheckboxes = driver.findElements(By.xpath("//input[position() > last()-5]")); System.out.println("Total checkboxes found: " + lastFiveCheckboxes.size()); for (WebElement e : lastFiveCheckboxes) { e.click(); // Putting sleep to show execution Thread.sleep(2000); } } } 

Output:-

Total checkboxes found: 10
Total checkboxes found: 5

If you like above post and feel it is worth to share to reach it to maximum people, please do.

#ThanksForReading

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 2: Installation Of TestNG In Eclipse ==> Download And Add To Build Path Way
Next Post: Ways of Handling StaleElementReferenceException Without PageFactory

Related Posts

#1. LeetCode| Java 11 | String Programs | Easy | Sorting the Sentence Uncategorized
image – Make Selenium Easy Uncategorized
January 20, 2019 – Make Selenium Easy Uncategorized
October 29, 2017 – Make Selenium Easy Uncategorized
TestNG Tutorials 68 : Sharing Data Between Tests in a Suite Using ISuite & ITestContext Uncategorized
TestNG Tutorials 69 : Rerun Failed Test Method Using IRetryAnalyzer Interface – Implementing With Listener – IAnnotationTransformer Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

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