πŸ’‘ If you like this website, please share it with your friends and network! πŸš€
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();