Selenium + Java · Lesson 28 of 44

0/44 done

Module 28: Calendars & Date Pickers

28. Automating Interactive Date Pickers & Month Navigation

16 minlesson

Your task

Implement selectDayInCalendar:

  1. Accept WebDriver driver and String dayNumber.
  2. Click By.xpath("//td[contains(@class, 'day') and text()='" + dayNumber + "']").

Write it in the editor, then press Check answer (Ctrl + Enter).

Date Picker Automation Pattern

java
// Open calendar widget:
driver.findElement(By.id("depart-date")).click();

// Navigate to target month:
String targetMonthYear = "December 2026";
while (!driver.findElement(By.className("calendar-header")).getText().contains(targetMonthYear)) {
    driver.findElement(By.className("next-month-btn")).click();
}

// Select target day:
String targetDay = "15";
driver.findElement(By.xpath("//div[@class='calendar-body']//td[text()='" + targetDay + "']")).click();

Key Takeaways & Walkthrough Summary

Date pickers can be tricky because dates change every month and year. In this lesson, you will learn how to navigate months in a loop and click dynamic day cells reliably. Let's master date pickers!

Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Can I just sendKeys to the date input instead of clicking the calendar?

If the input field is not readonly, sendKeys('12/15/2026') is much faster and more reliable! Always verify if direct text input is accepted.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip