top of page
Priyanka Nigam

Paging Through the Web: Basic and Advanced Selenium Techniques in Java for Handling Pagination in Various Scenarios-Part 1

We have often come across scenarios where it took longer times to load a certain page displaying huge datasets, when the need is to view only certain data. This not only hampers user experience with overwhelming data but also slows down the performance of the website. To overcome this problem, many websites use the concept of pagination.

So, what is Pagination and why it’s important? 

Well pagination, also known as paging, is the process of dividing a large set of datasets into smaller discrete pages to make it easier to navigate or handle data based on different filter conditions applied on them. So, by breaking large datasets into pages and categorizing them into sections in a structured manner, it provides more users control and facilitates quicker access to desired information.


In short, pagination create positive user experience, faster page loading, efficient navigation, and improved accessibility by loading and viewing only a specific portion of content at a time thus improving site performance and user experience.


Pagination typically involves adding links or buttons to the bottom of a page that allows users to access the next page of content. The pagination design varies in different website but the logic to navigate through pages remain the same. Basically, we need to find the total number of pages, get the products list in each page and then iterate through each page to get the product details.


Typical pagination design looks like this.


In this blog I’ll walk you through four different scenarios and technique to implement pagination logic in automation testing using selenium java. I’ve used Tarla Dalal and Amazon audible website to illustrate different approaches to achieve pagination.

To make the blog interesting and short I've divided the blog into two parts.


Scenario#1 When the total number of pages displayed is same as pagination count



Step1: Navigate to https://www.tarladalal.com/

Step2: Here I'm implementing logic to scrap recipes for PCOS conditions. From the home page click on Recipes tab and then click on PCOS (139 recipes)  under Healthy Recipes tab.


Selenium java code in eclipse:


Step 3: On PCOS recipes page, there are total of 140 recipes divided into 6 pages.


Step 4: To extract the required details from each recipe, present in all pages. We first need to locate the web element for pagination and then get the total number of pages (i.e. Page count), count of number of recipes present in each page (i.e. Recipe count per page). Iterate to get recipe details present in each page and then move to next page to get the recipe details on that page. Repeat the process until last page is reached.


See below the web elements and selenium/java code

//Web element for pagination


//web elements to get all the recipes link on each page (here it’s 24)

Code snippets: 

pagination.size() will give you the total page count (here it’s 6). Locate and get total recipe count in each page and iterate through each recipe link and store it in an array list. I have used advanced for loop to iterate through each recipe.

This is how the pagination logic is implemented here. It will extract recipe details on each page (incrementing the Page number) and loop will continue until the last page is reached.


Moving to next pagination logic when number of pages displayed are dynamic in nature and not all pages are displayed at a time. I will take the same website to demonstrate it. This time we will scrape recipes for Diabetes conditions.


Scenario#2 When all the pages are not displayed and we cannot get the total page count using pagination.size() method.

Here pagination.size() will return count =15 and not 24 as only 15 pages are displayed and remaining pages in dots(.) are hidden. So here we will slightly modify our logic to get the total page count. (see below the detailed explanation)


Step1: Navigate to https://www.tarladalal.com/

Step2: Here we are implementing logic to scrap recipes for Diabetes conditions. From the home page click on Recipes tab and then click on Diabetic recipes under Healthy Recipes tab.
















Selenium java code in eclipse:


Step 3: On diabetes recipes page, there are total of 573 recipes divided into 24 pages. Here if you see not all pages are displayed at a time but only 15 pages (1-5. 10-14 and 20-24) .So, we won’t be able to get the correct page count if we use pagination.size() which give us the  count as 15.(see below the web elements tag a (href for only 15 pages are displayed (1-5, 10-14 and 20-24))




Step 4: So, we will use the below formula to get the total page count

Total page count=Total recipe in all page/Count of recipe in each page


·        Get total recipe count from here and convert it to numeric

·        Locate and store web elements using class name rcc_recipename in a list and get the recipe count

·        Get the Total page count=Total recipe in all page/Count of recipe in each page (see below the code snippet)


Using Math.min() function we will get the minimum page count and then iterate it till the total page count to get details of each recipes in all pages(here 24 pages)

This is how pagination logic is developed in this scenario to iterate through all pages and get the recipe details.


Hope you find this blog interesting and informative. I will explain, how to automate pagination logic using while loop and Java stream(advanced concept) in my next blog.



Till then Happy Learning !!!!!


368 views

Recent Posts

See All
bottom of page