Selenium Date Pickers
Date pickers let visitors choose a day without typing. Travel sites, booking forms, and report filters all depend on them. Selenium meets three kinds of pickers, and each kind needs a different plan. Think of three ways to set a wall calendar: write the date on a card, flip pages until the month appears, or ask a helper to move the pointer. The right method depends on how the page builds the control.
Three Picker Types
| Type | Appearance | Best Approach |
|---|---|---|
| Native date input | Browser draws the calendar | Type the date with sendKeys |
| Custom calendar popup | Page draws a grid of days | Click month arrows and day cells |
| Read-only input | Typing is blocked | Set the value through JavaScript |
Calendar Diagram
< November 2026 > Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <-- target day cell: 18 22 23 24 25 26 27 28 29 30
Native Date Input
An input with type="date" accepts keystrokes in the format of the browser locale. Chrome in the United States expects month, day, year. Send the text without separators or with slashes, and the field fills each part.
WebElement dob = driver.findElement(By.id("birthdate"));
dob.sendKeys("11182026");Check the visible format first. A mismatch fills the wrong parts.
Plain Text Date Fields
Many pages use a normal text box with a calendar icon. These fields accept typed text when the page allows it.
WebElement date = driver.findElement(By.id("checkin"));
date.clear();
date.sendKeys("18/11/2026");Clicking a Custom Calendar
A custom picker opens after a click on the input. Follow the same steps a person follows: open the calendar, move to the right month, and click the day.
Click input -> calendar opens -> click arrow until header shows target month
-> click day cell -> calendar closes -> input shows date
driver.findElement(By.id("checkin")).click();
while (!driver.findElement(By.cssSelector(".month-title")).getText()
.equals("December 2026")) {
driver.findElement(By.cssSelector(".next-month")).click();
}
driver.findElement(By.xpath("//td[not(contains(@class,'disabled'))]/span[text()='18']")).click();The XPath skips greyed-out days from the previous or next month, which share the same numbers.
Calculating Dates in Code
Hard-coded dates expire. A test that books 18 November 2026 fails in 2027. Java's LocalDate class computes the date from today, and the test works on any day.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
LocalDate checkIn = LocalDate.now().plusDays(7);
String text = checkIn.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
driver.findElement(By.id("checkin")).sendKeys(text);
String day = String.valueOf(checkIn.getDayOfMonth());
Today + 7 days -> format text -> type into field
-> day number -> click matching cell in calendar
Read-Only Fields
Some pickers block typing to force a calendar click. A JavaScript snippet writes the value directly when no click path works.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].value='2026-11-18';", dateField);Use this shortcut only to prepare test data. A real check of the calendar widget should click through it like a visitor.
Validation Tests Worth Writing
- Past dates stay disabled in a booking calendar.
- The check-out date cannot precede the check-in date.
- A leap-day entry such as 29 February behaves correctly.
- The chosen date appears in the same format on the summary page.
- The calendar highlights today.
Waits and Stability
The calendar needs a moment to draw its grid. Wait for the day cells to appear before the click. An explicit wait on visibilityOfElementLocated avoids clicks on a half-drawn popup.
Key Points
- Identify the picker type before choosing a method.
- Type dates into native and text inputs.
- Click arrows and day cells in custom calendars.
- Compute dates from today to keep tests valid.
Real Booking Scenario
A hotel site asks for check-in and check-out dates. A thorough test chooses a check-in date one week ahead and a check-out date three nights later. The site then shows the number of nights and the total price. The test reads the night count and compares it with three. Date logic often hides defects at month ends and year ends, so add a second run that starts on the last day of a month, where the next-month arrow must move the calendar correctly.
Locale and Time Zone Awareness
The same date prints in different orders around the world: day first in India and Europe, month first in the United States. Read the expected format from the page, and build the text with a matching pattern. Time zones matter when the server and the test machine sit in different regions. A test that runs close to midnight may compute tomorrow on one machine and today on the other. Set an explicit zone with LocalDate.now(ZoneId.of("Asia/Kolkata")) when the site follows a fixed region.
