Selenium WebDriver
Day 26: Dynamic Table & Calendar Handling in Selenium
Master dynamic calendar navigation and date selection techniques using while loops and enhanced for loops in Selenium WebDriver
Today's Learning Objectives
Calendar Navigation
- • Dynamic calendar handling techniques
- • While loop for month/year navigation
- • Enhanced for loop for date selection
- • Real-world calendar implementations
Practical Applications
- • RedBus calendar automation
- • Goibibo flight booking dates
- • Complex XPath for date elements
- • Assignment: Complete booking workflow
Dynamic Calendar Handling Strategy
Step-by-Step Approach
Step 1: Click on Calendar Object
driver.findElement(By.xpath("//calendar-xpath")).click();
Step 2: Navigate to Target Month/Year
while(true) {
// Step 3: Capture current month and year
String actualMonthAndYear = driver.findElement(
By.xpath("//month-year-xpath")).getText();
// Step 4: Compare with expected month/year
if(actualMonthAndYear.equals("December 2025")) {
break;
} else {
// Step 5: Click next button
driver.findElement(By.xpath("//next-button-xpath")).click();
}
}
// Step 3: Capture current month and year
String actualMonthAndYear = driver.findElement(
By.xpath("//month-year-xpath")).getText();
// Step 4: Compare with expected month/year
if(actualMonthAndYear.equals("December 2025")) {
break;
} else {
// Step 5: Click next button
driver.findElement(By.xpath("//next-button-xpath")).click();
}
}
Step 6-10: Select Specific Date
// Step 6: Find all dates
List<WebElement> listDates = driver.findElements(
By.xpath("//date-elements-xpath"));
// Step 7: Iterate through dates
for(WebElement date : listDates) {
// Step 8: Capture actual date
String actualDate = date.getText();
// Step 9: Compare with expected date
if(actualDate.equals("10")) {
// Step 10: Click and break
date.click();
break;
}
}
List<WebElement> listDates = driver.findElements(
By.xpath("//date-elements-xpath"));
// Step 7: Iterate through dates
for(WebElement date : listDates) {
// Step 8: Capture actual date
String actualDate = date.getText();
// Step 9: Compare with expected date
if(actualDate.equals("10")) {
// Step 10: Click and break
date.click();
break;
}
}
Example 1: RedBus Calendar
Task: Select 15 December 2025
Website: https://www.redbus.in/
Complete Implementation
Java Code Implementation
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.redbus.in/");
// Step 1: Click on calendar
driver.findElement(By.xpath("//div[@class='dojWrapper___42f532']")).click();
// Step 2-5: Navigate to target month
while(true) {
String actualMonthAndYear = driver.findElement(
By.xpath("//p[@class='monthYear___fe577e']")).getText();
if(actualMonthAndYear.equals("April 2026")) {
break;
} else {
driver.findElement(By.xpath(
"//i[@class='icon icon-arrow arrow___19a681 right___65d0b5 ']")).click();
}
}
// Step 6-10: Select specific date
List<WebElement> allDates = driver.findElements(
By.xpath("//div[@class='dateWrap___8a859f']/descendant::span"));
for(WebElement date : allDates) {
String actualDate = date.getText();
if(actualDate.equals("7")) {
date.click();
break;
}
}
driver.manage().window().maximize();
driver.get("https://www.redbus.in/");
// Step 1: Click on calendar
driver.findElement(By.xpath("//div[@class='dojWrapper___42f532']")).click();
// Step 2-5: Navigate to target month
while(true) {
String actualMonthAndYear = driver.findElement(
By.xpath("//p[@class='monthYear___fe577e']")).getText();
if(actualMonthAndYear.equals("April 2026")) {
break;
} else {
driver.findElement(By.xpath(
"//i[@class='icon icon-arrow arrow___19a681 right___65d0b5 ']")).click();
}
}
// Step 6-10: Select specific date
List<WebElement> allDates = driver.findElements(
By.xpath("//div[@class='dateWrap___8a859f']/descendant::span"));
for(WebElement date : allDates) {
String actualDate = date.getText();
if(actualDate.equals("7")) {
date.click();
break;
}
}
Key XPath Locators
Calendar Trigger:
//div[@class='dojWrapper___42f532']
Month/Year Display:
//p[@class='monthYear___fe577e']
Next Button:
//i[@class='icon icon-arrow arrow___19a681 right___65d0b5 ']
Date Elements:
//div[@class='dateWrap___8a859f']/descendant::span
Example 2: Goibibo Calendar
Task: Select 24 March 2026
Website: https://www.goibibo.com/
Different Calendar Structure
Goibibo Implementation
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.goibibo.com/");
// Click on departure date
driver.findElement(By.xpath(
"//span[text()='Departure']/following-sibling::p[@class='sc-12foipm-4 czGBLf fswWidgetTitle']")).click();
// Navigate to target month
while(true) {
String actualMonthAndYear = driver.findElement(By.xpath(
"//div[@class='DayPicker-Month']/preceding-sibling::div/child::div[@class='DayPicker-Caption']/child::div")).getText();
if(actualMonthAndYear.equals("March 2026")) {
break;
} else {
driver.findElement(By.xpath(
"//span[@class='DayPicker-NavButton DayPicker-NavButton--next']")).click();
}
}
// Select specific date
List<WebElement> allDates = driver.findElements(By.xpath(
"//div[@class='DayPicker-Month']/preceding-sibling::div/child::div[@class='DayPicker-Body']/descendant::p"));
for(WebElement date : allDates) {
String actualDate = date.getText();
if(actualDate.equals("24")) {
date.click();
break;
}
}
driver.manage().window().maximize();
driver.get("https://www.goibibo.com/");
// Click on departure date
driver.findElement(By.xpath(
"//span[text()='Departure']/following-sibling::p[@class='sc-12foipm-4 czGBLf fswWidgetTitle']")).click();
// Navigate to target month
while(true) {
String actualMonthAndYear = driver.findElement(By.xpath(
"//div[@class='DayPicker-Month']/preceding-sibling::div/child::div[@class='DayPicker-Caption']/child::div")).getText();
if(actualMonthAndYear.equals("March 2026")) {
break;
} else {
driver.findElement(By.xpath(
"//span[@class='DayPicker-NavButton DayPicker-NavButton--next']")).click();
}
}
// Select specific date
List<WebElement> allDates = driver.findElements(By.xpath(
"//div[@class='DayPicker-Month']/preceding-sibling::div/child::div[@class='DayPicker-Body']/descendant::p"));
for(WebElement date : allDates) {
String actualDate = date.getText();
if(actualDate.equals("24")) {
date.click();
break;
}
}
Complex XPath Analysis
Departure Trigger:
//span[text()='Departure']/following-sibling::p
Month/Year Caption:
//div[@class='DayPicker-Caption']/child::div
Date Elements:
//div[@class='DayPicker-Body']/descendant::p
Assignment: Complete Goibibo Booking
Complete Flight Booking Workflow
Website: https://www.goibibo.com/
Required Tasks
- • Step 1: Click on round trip radio button
- • Step 2: Enter "Pune" in From dropdown
- • Step 3: Enter "Delhi" in To dropdown
- • Step 4: Select departure date: 28 November 2025
- • Step 5: Select return date: 4 December 2025
- • Step 6: Add 5 adults passengers
- • Step 7: Add 2 children
- • Step 8: Select Premium economy class
- • Step 9: Click on Done button
- • Step 10: Click on Search Flight button
Complex XPath for Date Selection
Advanced Date Locator:
//div[@class='rdrMonth']/preceding-sibling::div/descendant::button[@class='rdrDay' or @class='rdrDay rdrDayWeekend rdrDayEndOfWeek' or @class='rdrDay rdrDayWeekend rdrDayStartOfWeek' or @class='rdrDay rdrDayStartOfMonth' or @class='rdrDay rdrDayEndOfMonth']/descendant::span[@class='date']
This XPath handles multiple date button classes for comprehensive date selection
Best Practices for Calendar Handling
✅ Do's
- • Always use while(true) for month navigation
- • Break the loop immediately after finding target
- • Use Thread.sleep() for calendar loading
- • Capture month/year text for comparison
- • Use enhanced for loop for date iteration
- • Handle different calendar structures
❌ Don'ts
- • Don't forget to break while loop
- • Avoid hardcoded date selections
- • Don't assume calendar structure
- • Avoid clicking without verification
- • Don't use fixed wait times
- • Avoid complex nested loops
Knowledge Check
Question 1 of 5
What is the correct approach for navigating to a specific month in a dynamic calendar?
Key Points Summary
Calendar Navigation Steps
- • Step 1: Click calendar trigger element
- • Step 2-5: while(true) loop for month navigation
- • Step 6: Find all date elements using findElements()
- • Step 7-10: Enhanced for loop for date selection
Real-World Applications
- • RedBus: Bus booking date selection
- • Goibibo: Flight departure/return dates
- • Complex XPath: Multiple date button classes
- • Assignment: Complete booking workflow