Back to All Scenarios
Scenario 28 of 100
Calendars
Advanced
Selecting Dates from a Custom Calendar Widget
π
Scenario Overview
Selecting Dates from a Custom Calendar Widget
Key Takeaways & Cheat Sheet
- βAvoid fragile relative click pathways for months and days when possible
- βCheck if the date field is editable and allows direct sendKeys input
- βClick date headers to switch months dynamically until target is reached
- βUse text-based XPaths to target specific day divs safely
Short Direct Answer
Custom calendars (date-pickers) are complex widgets built from divs, spans, and tables. The best strategy is to check if the input field is writableβif so, clear it and send the date string directly. If it is read-only, click the calendar host, navigate through the months using navigation buttons, and use a relative text-based xpath to click the target day.
β οΈ Senior Warning (Red Flag)
Never use hardcoded grid indexes (like clicking index 15) to select dates. Calendar layouts shift dynamically depending on month lengths and starting days of the week.
π‘ STAR Deep Dive Explanation & Pro Tip
Writing directly using sendKeys() is much faster and reduces suite execution time significantly compared to rendering and clicking through calendar months.
SeleniumAutomation.java
Selenium 4 + Java// β
Best Practice 1: Send date string directly if field is writable
WebElement dateInput = driver.findElement(By.id("departure-date"));
dateInput.clear();
dateInput.sendKeys("2026-12-25");
// β
Strategy 2: Physical navigation of dynamic calendar
driver.findElement(By.id("calendar-trigger")).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
// Loop until target month is visible
while (!driver.findElement(By.className("calendar-title")).getText().contains("December 2026")) {
driver.findElement(By.className("calendar-next-btn")).click();
}
// Click the specific day using stable text-match
driver.findElement(By.xpath("//div[@class='calendar-day' and text()='25']")).click();