Back to All Scenarios
Scenario 30 of 100
Calendars
Advanced
Navigating Calendars that Show Previous Months Only
βͺScenario Overview
Navigating Calendars that Show Previous Months Only
Key Takeaways & Cheat Sheet
- βLocate and verify back-navigation controls (prev-month buttons)
- βCheck current calendar title header string values dynamically
- βImplement a loop switching back one month at a time until matched
- βLimit your loop with a safety counter to avoid infinite backward loops
Short Direct Answer
To automate select-past-date widgets (such as selecting historic date of birth or audit metrics), you must navigate backwards. Implement a loop that checks the currently displayed month header. If it does not match your target month, click the "Previous" month button and check again, exiting the loop only when the headers match.
β οΈ Senior Warning (Red Flag)
Do not hardcode your navigation clicks. If your test runs in a new month, the number of clicks required to reach a past date shifts, causing locator failures.
π‘ STAR Deep Dive Explanation & Pro Tip
Always verify the calendar title. If the calendar is opened inside a modal, ensure the modal has completed rendering before clicking the previous arrow to avoid click misses.
SeleniumAutomation.java
Selenium 4 + Java// β
Dynamic backward calendar navigation
public void selectPastDate(String targetMonthYear, String targetDay) {
driver.findElement(By.id("birthdate-calendar")).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
int safetyCounter = 0;
while (safetyCounter < 24) { // Limit to 2 years back max
String currentMonthHeader = driver.findElement(By.className("cal-month-header")).getText();
if (currentMonthHeader.trim().equalsIgnoreCase(targetMonthYear)) {
break; // Found correct month
}
// Click Previous month arrow
driver.findElement(By.className("cal-prev-arrow")).click();
safetyCounter++;
}
// Click the specific day node
driver.findElement(By.xpath("//span[@class='day' and text()='" + targetDay + "']")).click();
}