Date Pickers in Selenium WebDriver
A Date Picker is a user-interface component that allows users to select a date from a calendar instead of manually entering the date. Date pickers are commonly used in flight booking, hotel reservation, appointment scheduling, registration forms, banking applications, e-commerce applications, report filters, insurance systems, and many other web applications.
In Selenium WebDriver automation, handling date pickers requires understanding how the calendar is implemented in the HTML DOM. A date picker may be a standard HTML <input type="date">, a custom calendar created with HTML and JavaScript, a date-range picker, a date-time picker, or a calendar component provided by a frontend framework.
Selenium training commonly covers WebDriver, locators, web elements, browser interactions, waits, dynamic elements, forms, dropdowns, TestNG, Page Object Model, and practical automation scenarios. Date Picker handling combines many of these Selenium concepts in one real-world automation task.
Course Resources: Selenium Training Course | Register for Course Demo
1. What Is a Date Picker?
A date picker is a graphical calendar component that allows a user to choose a specific date. Instead of manually typing a date, the user can open a calendar, navigate between months or years, and select a day.
Click Date Field
↓
Calendar Opens
↓
Read Current Month/Year
↓
Navigate to Required Month/Year
↓
Select Required Day
↓
Date Appears in Input
↓
Verify Selected Date
2. Why Date Pickers Are Important in Selenium?
Date selection is a common automation requirement because many business applications depend on dates.
- Flight departure and return dates
- Hotel check-in and check-out dates
- Doctor appointment dates
- Job application dates
- Bank transaction dates
- Insurance policy dates
- Delivery dates
- Report filtering dates
- Event registration dates
- Employee joining and leave dates
3. Types of Date Pickers
| Type | Description |
| HTML Date Input | Uses the standard <input type="date"> element. |
| Custom Date Picker | A calendar created using buttons, divs, spans, tables, or other HTML elements. |
| JavaScript Date Picker | A calendar controlled by JavaScript or a UI library. |
| Month/Year Picker | Allows users to select a month and year. |
| Date Range Picker | Allows users to select a start date and an end date. |
| Date-Time Picker | Allows users to select both date and time. |
| Multi-Date Picker | Allows multiple dates to be selected. |
| Searchable Date Picker | Allows dates to be searched or typed manually. |
4. Standard HTML Date Input
A basic HTML date input may look like this:
<input type="date" id="dob">
This is different from a custom calendar because the browser provides the date-selection interface.
5. Handling HTML Date Input
When the date picker is a standard input element, Selenium can interact directly with the input.
WebElement dateField = driver.findElement(
By.id("dob")
);
dateField.sendKeys("03/15/2026");
The accepted date format can depend on the browser and implementation, so the expected format should be verified for the application under test.
6. Using JavaScript With HTML Date Input
In some situations, JavaScript can be used to set the value of a standard date input.
WebElement dateField = driver.findElement(
By.id("dob")
);
JavascriptExecutor js =
(JavascriptExecutor) driver;
js.executeScript(
"arguments[0].value='2026-03-15';",
dateField
);
When using JavaScript, verify that the application's event handlers recognize the changed value correctly.
7. Custom Date Picker
A custom date picker may contain separate controls for the previous month, next month, month name, year, and individual days.
<div class="datepicker">
<button class="previous">Previous</button>
<span class="month-year">March 2026</span>
<button class="next">Next</button>
<div class="days">
<button>1</button>
<button>2</button>
<button>3</button>
</div>
</div>
In this situation, Selenium interacts with the individual elements instead of treating the entire calendar as one control.
8. Basic Custom Date Picker Flow
Locate Date Field
↓
Click Date Field
↓
Wait for Calendar
↓
Read Month and Year
↓
Compare With Target
↓
Click Previous/Next
↓
Locate Target Day
↓
Click Target Day
↓
Verify Date
9. Open the Date Picker
driver.findElement(
By.id("departureDate")
).click();
The click should open the calendar component.
10. Wait for the Calendar
Dynamic date pickers may take some time to become visible. Explicit waits are generally more reliable than arbitrary sleep statements.
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".datepicker")
)
);
11. Locate Month and Year
Many date pickers display the currently selected month and year in a header.
WebElement monthYear = driver.findElement(
By.cssSelector(".month-year")
);
String displayedMonthYear =
monthYear.getText();
System.out.println(displayedMonthYear);
12. Example Calendar Header
<div class="calendar-header">
<button class="prev"><</button>
<span class="month-year">
March 2026
</span>
<button class="next">></button>
</div>
13. Locate Next Month Button
WebElement nextButton = driver.findElement(
By.cssSelector(".next")
);
nextButton.click();
14. Locate Previous Month Button
WebElement previousButton = driver.findElement(
By.cssSelector(".prev")
);
previousButton.click();
15. Navigate to a Future Month
If the required date is in a future month, the next-month button can be clicked repeatedly until the target month and year are displayed.
while (!driver.findElement(
By.cssSelector(".month-year")
).getText().equals("June 2026")) {
driver.findElement(
By.cssSelector(".next")
).click();
}
16. Navigate to a Previous Month
If the application allows past dates, the previous-month button can be used.
while (!driver.findElement(
By.cssSelector(".month-year")
).getText().equals("January 2026")) {
driver.findElement(
By.cssSelector(".prev")
).click();
}
17. Why Month and Year Validation Is Important
Selecting a day number without first validating the month and year can cause the wrong date to be selected. For example, a calendar may display days belonging to the previous or next month inside the current month's grid.
Current Display:
March 2026
Target:
March 15, 2026
Correct Process:
Verify March 2026
↓
Locate 15
↓
Select 15
18. Selecting a Day
Once the required month and year are displayed, locate the target day.
driver.findElement(
By.xpath("//button[normalize-space()='15']")
).click();
This approach should only be used when the day number is unique within the relevant calendar.
19. Problem With Selecting Only the Day Number
A locator such as //button[normalize-space()='15'] may match more than one element if the calendar contains dates from adjacent months.
A better strategy is to use a stable date attribute when the application provides one.
20. Using data-date Attribute
<button class="day"
data-date="2026-03-15">
15
</button>
Selenium can directly identify the exact date:
driver.findElement(
By.cssSelector(
".day[data-date='2026-03-15']"
)
).click();
21. XPath With data-date
driver.findElement(
By.xpath(
"//button[@data-date='2026-03-15']"
)
).click();
22. Selecting a Date Using aria-label
Accessible date pickers may expose the complete date through an ARIA attribute.
<button role="gridcell"
aria-label="March 15, 2026">
15
</button>
driver.findElement(
By.cssSelector(
"[aria-label='March 15, 2026']"
)
).click();
23. Handling Date Picker Tables
Some date pickers use an HTML table to represent the calendar.
<table class="calendar">
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
driver.findElement(
By.xpath(
"//table[@class='calendar']//td[normalize-space()='15']"
)
).click();
24. Handling Disabled Dates
Date pickers often disable dates that cannot be selected.
<button class="day disabled"
disabled>
10
</button>
The test should verify that disabled dates behave according to the application's requirements.
25. Selecting Only Enabled Dates
WebElement date = driver.findElement(
By.xpath(
"//button[normalize-space()='15' and not(@disabled)]"
)
);
date.click();
26. Past Dates
Booking and appointment systems frequently restrict past dates.
Past Dates
↓
Disabled
Today
↓
Available or Restricted
Future Dates
↓
Available
The exact behavior depends on the application requirements.
27. Future Date Selection
A typical future-date automation flow is:
- Open the date picker.
- Read the current month and year.
- Compare them with the target month and year.
- Click the next-month button as required.
- Verify the target month and year.
- Select the target day.
- Verify the selected value.
28. Date Picker With Separate Month and Year Controls
Some calendars provide separate month and year selectors.
<select class="month">
<option>January</option>
<option>February</option>
</select>
<select class="year">
<option>2026</option>
<option>2027</option>
</select>
If these are actual HTML <select> elements, Selenium's Select class can be used.
29. Selecting Month With Select
Select month = new Select(
driver.findElement(By.cssSelector(".month"))
);
month.selectByVisibleText("June");
30. Selecting Year With Select
Select year = new Select(
driver.findElement(By.cssSelector(".year"))
);
year.selectByVisibleText("2026");
31. Custom Month Dropdown
If the month control is not an actual HTML <select>, do not use Selenium's Select class.
driver.findElement(
By.id("monthDropdown")
).click();
driver.findElement(
By.xpath("//div[normalize-space()='June']")
).click();
32. Custom Year Dropdown
driver.findElement(
By.id("yearDropdown")
).click();
driver.findElement(
By.xpath("//div[normalize-space()='2026']")
).click();
33. Date Range Picker
A date range picker allows users to select two dates, generally a start date and an end date.
Start Date → 10 June 2026
End Date → 15 June 2026
Date range pickers are common in travel websites, hotel booking systems, analytics dashboards, and reporting applications.
34. Date Range Selection Flow
Open Calendar
↓
Select Start Date
↓
Select End Date
↓
Calendar Updates Range
↓
Verify Start Date
↓
Verify End Date
35. Selecting Start Date
driver.findElement(
By.cssSelector(
".day[data-date='2026-06-10']"
)
).click();
36. Selecting End Date
driver.findElement(
By.cssSelector(
".day[data-date='2026-06-15']"
)
).click();
37. Verifying Date Range
String startDate = driver.findElement(
By.id("startDate")
).getAttribute("value");
String endDate = driver.findElement(
By.id("endDate")
).getAttribute("value");
System.out.println(startDate);
System.out.println(endDate);
38. Date-Time Picker
A date-time picker combines date selection with time selection.
Select Date
↓
Select Time
↓
Select AM/PM if required
↓
Confirm
39. Date-Time Picker Example
driver.findElement(
By.id("dateField")
).click();
driver.findElement(
By.cssSelector(
".day[data-date='2026-06-15']"
)
).click();
driver.findElement(
By.id("timeField")
).click();
driver.findElement(
By.xpath("//div[normalize-space()='10:30 AM']")
).click();
40. Searchable Date Picker
Some date controls allow the user to type a date directly.
WebElement input = driver.findElement(
By.id("dateSearch")
);
input.clear();
input.sendKeys("06/15/2026");
41. Common Date Formats
| Format | Example |
| MM/dd/yyyy | 06/15/2026 |
| dd/MM/yyyy | 15/06/2026 |
| yyyy-MM-dd | 2026-06-15 |
| dd-MMM-yyyy | 15-Jun-2026 |
| MMM dd, yyyy | Jun 15, 2026 |
The automation script should use the format expected by the application.
42. Java LocalDate
Java's LocalDate class can be used to represent target dates in automation scripts.
import java.time.LocalDate;
LocalDate targetDate =
LocalDate.of(2026, 6, 15);
System.out.println(targetDate);
43. Getting Current Date
LocalDate today = LocalDate.now();
System.out.println(today);
44. Getting Future Date
LocalDate futureDate =
LocalDate.now().plusDays(10);
System.out.println(futureDate);
45. Getting Future Month
LocalDate futureDate =
LocalDate.now().plusMonths(2);
System.out.println(futureDate);
46. Getting Previous Date
LocalDate previousDate =
LocalDate.now().minusDays(5);
System.out.println(previousDate);
47. Extracting Day, Month and Year
LocalDate date =
LocalDate.of(2026, 6, 15);
int day = date.getDayOfMonth();
int month = date.getMonthValue();
int year = date.getYear();
System.out.println(day);
System.out.println(month);
System.out.println(year);
48. Formatting LocalDate
import java.time.format.DateTimeFormatter;
LocalDate date =
LocalDate.of(2026, 6, 15);
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formattedDate =
date.format(formatter);
System.out.println(formattedDate);
49. Selecting a Date Using LocalDate
LocalDate targetDate =
LocalDate.of(2026, 6, 15);
String dateValue =
targetDate.toString();
driver.findElement(
By.cssSelector(
".day[data-date='" + dateValue + "']"
)
).click();
50. Dynamic Date Picker
A dynamic date picker can change its DOM after every month navigation. This means previously stored WebElements may become invalid.
Open Calendar
↓
Click Next
↓
DOM Refreshes
↓
Old Elements Become Stale
↓
Locate New Elements
↓
Continue Automation
51. StaleElementReferenceException
StaleElementReferenceException may occur when an element previously located by Selenium is removed or replaced in the DOM.
Date pickers commonly cause this problem because clicking next or previous month can rebuild the calendar.
52. Avoiding Stale Elements
Instead of keeping a WebElement reference throughout the entire navigation process, locate the element again after the calendar changes.
while (!driver.findElement(
By.cssSelector(".month-year")
).getText().equals("June 2026")) {
driver.findElement(
By.cssSelector(".next")
).click();
}
53. Explicit Wait for Target Date
WebElement date = wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(
".day[data-date='2026-06-15']"
)
)
);
date.click();
54. Waiting for Target Month
wait.until(
ExpectedConditions.textToBePresentInElementLocated(
By.cssSelector(".month-year"),
"June 2026"
)
);
55. Handling Calendar Animation
Some date pickers animate when changing months. Clicking a date immediately after the navigation may fail if the calendar is still transitioning.
Wait for a specific calendar state, such as the target month being displayed, instead of relying on an arbitrary delay.
56. Handling ElementClickInterceptedException
ElementClickInterceptedException can occur when an overlay, animation, popup, or another element blocks the date.
- Wait for the calendar to become stable.
- Wait for overlays to disappear.
- Scroll the date into view.
- Verify that the date is clickable.
- Use JavaScript as a fallback when appropriate.
57. Scrolling Date Into View
WebElement date = driver.findElement(
By.cssSelector(
".day[data-date='2026-06-15']"
)
);
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollIntoView({block:'center'});",
date
);
date.click();
58. JavaScript Click
WebElement date = driver.findElement(
By.cssSelector(
".day[data-date='2026-06-15']"
)
);
JavascriptExecutor js =
(JavascriptExecutor) driver;
js.executeScript(
"arguments[0].click();",
date
);
Normal WebDriver interaction should generally be preferred before using JavaScript as a fallback.
59. Handling Today's Date
Some date pickers identify today's date using a CSS class or attribute.
<button class="day today"
data-date="2026-06-15">
15
</button>
driver.findElement(
By.cssSelector(".day.today")
).click();
60. Verifying Today's Date
WebElement today = driver.findElement(
By.cssSelector(".day.today")
);
String dateValue =
today.getAttribute("data-date");
System.out.println(dateValue);
61. Verifying Selected Date
After selecting a date, verify that the expected value appears in the input.
String actualDate = driver.findElement(
By.id("departureDate")
).getAttribute("value");
System.out.println(actualDate);
62. TestNG Assertion
String actualDate = driver.findElement(
By.id("departureDate")
).getAttribute("value");
Assert.assertEquals(
actualDate,
"06/15/2026"
);
63. Date Picker in Flight Booking
Flight booking applications commonly require departure and return dates.
Click Departure Date
↓
Open Calendar
↓
Navigate to Target Month
↓
Select Departure Date
↓
Click Return Date
↓
Select Return Date
↓
Verify Both Dates
↓
Search Flights
64. Flight Booking Example
driver.findElement(
By.id("departureDate")
).click();
wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(
".day[data-date='2026-06-10']"
)
)
).click();
driver.findElement(
By.id("returnDate")
).click();
wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(
".day[data-date='2026-06-15']"
)
)
).click();
65. Hotel Booking Date Picker
Hotel booking applications generally require a check-in and check-out date.
Check-in → 10 June 2026
Check-out → 15 June 2026
The test should verify that both dates are selected correctly and that the resulting date range follows the application's validation rules.
66. Appointment Date Picker
Appointment systems may restrict dates based on availability.
Available Date
↓
Selectable
Unavailable Date
↓
Disabled
Past Date
↓
Restricted
67. Available Date
<button class="day available"
data-date="2026-06-10">
10
</button>
driver.findElement(
By.cssSelector(
".day.available[data-date='2026-06-10']"
)
).click();
68. Unavailable Date
<button class="day unavailable"
disabled>
11
</button>
Negative testing should verify that the application prevents selection of unavailable dates.
69. Multiple Date Pickers on One Page
A page may contain several date controls such as departure date, return date, start date, end date, or date-of-birth.
WebElement departure = driver.findElement(
By.id("departureDate")
);
WebElement returnDate = driver.findElement(
By.id("returnDate")
);
departure.click();
Use specific and scoped locators to avoid interacting with the wrong calendar.
70. Scoped Calendar Locator
WebElement calendar = driver.findElement(
By.id("departureCalendar")
);
calendar.findElement(
By.cssSelector(
".day[data-date='2026-06-10']"
)
).click();
71. Multiple Calendars Displayed Together
Some applications display two months at the same time.
June 2026 July 2026
[Calendar 1] [Calendar 2]
The automation should identify the correct calendar by reading its month and year before selecting the day.
72. Finding the Correct Calendar
List<WebElement> calendars =
driver.findElements(
By.cssSelector(".calendar")
);
for (WebElement calendar : calendars) {
String header = calendar.findElement(
By.cssSelector(".month-year")
).getText();
System.out.println(header);
}
73. Selecting Date From Correct Calendar
List<WebElement> calendars =
driver.findElements(
By.cssSelector(".calendar")
);
for (WebElement calendar : calendars) {
String header = calendar.findElement(
By.cssSelector(".month-year")
).getText();
if (header.equals("June 2026")) {
calendar.findElement(
By.cssSelector(
".day[data-date='2026-06-15']"
)
).click();
break;
}
}
74. Dynamic IDs in Date Pickers
Some date elements have dynamically generated IDs.
<button id="date-872341">15</button>
Stable attributes such as data-date, aria-label, or meaningful classes are generally preferable.
75. starts-with() With Dynamic ID
driver.findElement(
By.xpath(
"//button[starts-with(@id,'date-') and normalize-space()='15']"
)
).click();
76. contains() With Dynamic ID
driver.findElement(
By.xpath(
"//button[contains(@id,'date-') and normalize-space()='15']"
)
).click();
77. ARIA Attributes
Modern date pickers may use accessibility attributes to describe dates and selection states.
<button role="gridcell"
aria-label="June 15, 2026"
aria-selected="false">
15
</button>
78. Using aria-selected
String selected =
date.getAttribute("aria-selected");
if ("true".equals(selected)) {
System.out.println(
"Date is selected"
);
}
79. Keyboard Navigation
Some date pickers support keyboard controls such as arrow keys, Enter, Escape, Page Up, and Page Down.
WebElement dateField = driver.findElement(
By.id("dateField")
);
dateField.click();
dateField.sendKeys(Keys.ARROW_RIGHT);
dateField.sendKeys(Keys.ENTER);
80. Closing Calendar With Escape
dateField.sendKeys(Keys.ESCAPE);
81. Date Picker Inside an iframe
If the date picker exists inside an iframe, Selenium must switch into the iframe before interacting with its elements.
WebElement frame = driver.findElement(
By.cssSelector("iframe")
);
driver.switchTo().frame(frame);
driver.findElement(
By.id("dateField")
).click();
driver.switchTo().defaultContent();
82. Date Picker Inside a Modal
WebElement modal = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".modal")
)
);
modal.findElement(
By.id("dateField")
).click();
modal.findElement(
By.cssSelector(
".day[data-date='2026-06-15']"
)
).click();
83. Date Picker Inside Shadow DOM
If the date picker is inside Shadow DOM, Selenium can obtain the shadow root and locate elements within it.
WebElement host = driver.findElement(
By.cssSelector("custom-date-picker")
);
SearchContext shadowRoot =
host.getShadowRoot();
shadowRoot.findElement(
By.cssSelector("input")
).click();
84. Date Picker With Icons
Previous and next controls may use icons inside buttons.
<button class="calendar-next">
<span class="icon-arrow-right"></span>
</button>
Prefer locating the clickable button rather than its nested icon.
driver.findElement(
By.cssSelector(".calendar-next")
).click();
85. Locator Strategy for Date Pickers
| Locator | Usage |
| ID | Unique date field or calendar element |
| CSS Selector | Stable classes and attributes |
| XPath | Complex relationships and text-based selection |
| data-date | Exact date selection |
| aria-label | Accessible date representation |
| Role | Accessible calendar components |
86. Recommended Date Locator
If the application exposes an exact date through a stable attribute, it is generally preferable to selecting only by the visible day number.
By.cssSelector(
".day[data-date='2026-06-15']"
);
87. Date Picker Exceptions
| Exception | Possible Cause |
| NoSuchElementException | Incorrect locator or element is not present. |
| TimeoutException | Required element did not appear within the wait period. |
| StaleElementReferenceException | Calendar DOM was refreshed. |
| ElementClickInterceptedException | Overlay or animation blocked the click. |
| ElementNotInteractableException | Element exists but cannot currently be interacted with. |
88. Handling NoSuchElementException
Verify the locator, calendar visibility, frame context, Shadow DOM context, and page state before interacting with the element.
89. Handling TimeoutException
Use an appropriate explicit wait and confirm that the expected calendar state actually occurs.
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".datepicker")
)
);
90. Handling StaleElementReferenceException
Re-locate the calendar element after navigation because the DOM may have changed.
driver.findElement(
By.cssSelector(".month-year")
).getText();
91. Handling Click Intercepted Exception
Wait for animations or overlays to finish before clicking the target date.
wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(
".day[data-date='2026-06-15']"
)
)
).click();
92. Common Mistakes
- Assuming every date picker is a standard HTML date input.
- Using the
Select class on custom calendar elements.
- Selecting a day without opening the calendar.
- Selecting a day without checking the month and year.
- Using only the visible day number when duplicate dates exist.
- Ignoring disabled dates.
- Using hard-coded waits unnecessarily.
- Keeping stale WebElement references after navigation.
- Ignoring iframe boundaries.
- Ignoring Shadow DOM.
- Using unstable generated IDs.
- Not verifying the selected date.
93. Best Practices
- Inspect the date picker DOM before creating locators.
- Identify whether the date picker is native or custom.
- Prefer stable attributes such as
data-date.
- Use explicit waits for dynamic calendars.
- Validate the displayed month and year.
- Re-locate elements after DOM updates.
- Scope locators when multiple calendars exist.
- Handle disabled and unavailable dates.
- Verify the final input value.
- Use reusable date-picker methods.
- Use Page Object Model in larger frameworks.
- Use JavaScript only when normal WebDriver interaction is not suitable.
94. Date Picker Automation Strategy
1. Inspect HTML
↓
2. Identify Date Picker Type
↓
3. Locate Date Field
↓
4. Open Calendar
↓
5. Locate Month/Year
↓
6. Navigate to Target Month
↓
7. Locate Target Date
↓
8. Select Date
↓
9. Verify Date
↓
10. Continue Test
95. Practical Test Scenario
Scenario: Verify that a user can select 15 June 2026 from a custom date picker.
- Launch the browser.
- Open the application.
- Navigate to the required form.
- Click the date field.
- Wait for the calendar.
- Read the current month and year.
- Navigate to June 2026.
- Locate 15 June 2026.
- Click the date.
- Verify the date field.
96. Date Picker Test Case
| Step | Action | Expected Result |
| 1 | Open application | Application loads successfully. |
| 2 | Click date field | Calendar opens. |
| 3 | Navigate to June 2026 | June 2026 is displayed. |
| 4 | Select June 15 | June 15 is selected. |
| 5 | Verify field | Expected date is displayed. |
97. Complete Custom Date Picker Example
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class DatePickerTest {
private WebDriver driver;
private WebDriverWait wait;
@BeforeMethod
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
driver.get("https://example.com");
}
@Test
public void selectDate() {
driver.findElement(
By.id("departureDate")
).click();
wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".datepicker")
)
);
while (!driver.findElement(
By.cssSelector(".month-year")
).getText().equals("June 2026")) {
driver.findElement(
By.cssSelector(".next")
).click();
}
wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(
".day[data-date='2026-06-15']"
)
)
).click();
String actualDate = driver.findElement(
By.id("departureDate")
).getAttribute("value");
Assert.assertEquals(
actualDate,
"06/15/2026"
);
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
98. Reusable Date Selection Method
public void selectDate(String date) {
By dateLocator = By.cssSelector(
".day[data-date='" + date + "']"
);
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.elementToBeClickable(
dateLocator
)
).click();
}
99. Using Reusable Date Method
selectDate("2026-06-15");
100. Date Picker Utility Method
public void selectDate(
By dateField,
String date) {
driver.findElement(dateField).click();
By dateLocator = By.cssSelector(
".day[data-date='" + date + "']"
);
WebDriverWait wait = new WebDriverWait(
driver,
Duration.ofSeconds(10)
);
wait.until(
ExpectedConditions.elementToBeClickable(
dateLocator
)
).click();
}
101. Using Date Picker Utility
selectDate(
By.id("departureDate"),
"2026-06-15"
);
102. Page Object Model for Date Picker
public class BookingPage {
private WebDriver driver;
private By departureDate =
By.id("departureDate");
private By nextMonth =
By.cssSelector(".next");
public BookingPage(WebDriver driver) {
this.driver = driver;
}
public void openDepartureCalendar() {
driver.findElement(
departureDate
).click();
}
public void nextMonth() {
driver.findElement(
nextMonth
).click();
}
public void selectDate(String date) {
By dateLocator = By.cssSelector(
".day[data-date='" + date + "']"
);
driver.findElement(
dateLocator
).click();
}
}
103. Using Page Object Date Picker
BookingPage bookingPage =
new BookingPage(driver);
bookingPage.openDepartureCalendar();
bookingPage.selectDate(
"2026-06-15"
);
104. Negative Testing
Date picker automation should also test invalid scenarios.
- Selecting a disabled date.
- Selecting a past date when past dates are restricted.
- Selecting an unavailable appointment date.
- Selecting an end date before the start date.
- Leaving the date field empty.
- Entering an invalid date format.
- Closing the calendar without selecting a date.
- Changing the month without selecting a date.
105. Invalid Date Range
A range picker may prevent an end date from being earlier than the start date.
Start Date = 20 June 2026
End Date = 15 June 2026
The application may display a validation message, prevent the selection, or apply another business rule.
106. Verifying Validation Message
WebElement error = wait.until(
ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".date-error")
)
);
System.out.println(
error.getText()
);
107. Date Picker and Regression Testing
Date picker functionality should be included in regression testing whenever calendar-related functionality changes. Regression tests can cover date navigation, date selection, disabled dates, range selection, validation, and form submission.
108. Date Picker and Data-Driven Testing
Different dates can be provided as test data.
String[] dates = {
"2026-06-10",
"2026-06-15",
"2026-07-01"
};
for (String date : dates) {
System.out.println(date);
}
109. Date Picker With External Test Data
Dates can be stored in Excel, CSV, JSON, databases, or other test-data sources and passed into a reusable date-selection method.
String testDate = "2026-06-15";
selectDate(
By.id("departureDate"),
testDate
);
110. API-Driven Date Availability
Some applications load available dates dynamically from a backend API.
Open Calendar
↓
Application Requests Availability
↓
API Returns Available Dates
↓
Calendar Updates
↓
Available Dates Become Selectable
↓
Select Date
111. Waiting for API-Loaded Dates
wait.until(
ExpectedConditions.invisibilityOfElementLocated(
By.cssSelector(".loading-spinner")
)
);
wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(
".day[data-date='2026-06-15']"
)
)
).click();
112. Date Picker Accessibility Testing
When accessibility is part of the test requirements, automation can verify attributes such as:
- ARIA roles
- ARIA labels
- ARIA selected state
- Disabled state
- Keyboard navigation
- Accessible month and year information
113. Date Picker Locator Comparison
| Approach | Example | Use |
| ID | By.id("dateField") | Unique date input |
| CSS Class | .datepicker | Calendar component |
| CSS Attribute | [data-date='2026-06-15'] | Exact date |
| XPath Text | //button[normalize-space()='15'] | Visible day |
| ARIA Label | [aria-label='June 15, 2026'] | Accessible date |
114. Date Picker Best Practices
- Inspect the HTML structure before writing automation.
- Determine whether the date picker is native or custom.
- Prefer stable IDs and data attributes.
- Prefer exact date attributes over day-only locators.
- Use explicit waits for dynamic elements.
- Validate the month and year before selecting the day.
- Re-locate elements after calendar navigation.
- Scope locators when multiple calendars exist.
- Test disabled and unavailable dates.
- Verify the final date value.
- Create reusable date-picker utility methods.
- Use Page Object Model for maintainable frameworks.
- Use Java date APIs for dynamic test data.
115. Practical Mini Project
Project: Automate a hotel booking date range.
Requirement: Select check-in as 10 June 2026 and check-out as 15 June 2026.
- Launch the browser.
- Open the hotel booking application.
- Click the check-in field.
- Wait for the calendar.
- Navigate to June 2026.
- Select 10 June 2026.
- Click the check-out field.
- Select 15 June 2026.
- Verify both values.
- Click Search.
- Verify the search results.
116. Mini Project Code
driver.findElement(
By.id("checkIn")
).click();
wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(
".day[data-date='2026-06-10']"
)
)
).click();
driver.findElement(
By.id("checkOut")
).click();
wait.until(
ExpectedConditions.elementToBeClickable(
By.cssSelector(
".day[data-date='2026-06-15']"
)
)
).click();
String checkIn = driver.findElement(
By.id("checkIn")
).getAttribute("value");
String checkOut = driver.findElement(
By.id("checkOut")
).getAttribute("value");
System.out.println(
"Check-in: " + checkIn
);
System.out.println(
"Check-out: " + checkOut
);
117. Real-World Applications
| Application | Date Picker Usage |
| Flight Booking | Departure and return dates |
| Hotel Booking | Check-in and check-out dates |
| Healthcare | Appointment dates |
| Banking | Transaction and statement dates |
| E-Commerce | Delivery dates |
| HR Systems | Joining and leave dates |
| Analytics | Report date ranges |
| Insurance | Policy dates |
| Education | Exam and enrollment dates |
118. Interview Question: What Is a Date Picker?
A date picker is a UI component that allows users to select a date through a calendar interface. It can be implemented as a native HTML date input or as a custom JavaScript-based calendar.
119. Interview Question: How Do You Handle a Custom Date Picker?
Inspect the calendar DOM, click the date field, wait for the calendar, identify the displayed month and year, navigate to the target month, locate the required day, click it, and verify the selected value.
120. Interview Question: How Do You Select a Future Date?
Open the calendar, compare the current month and year with the target month and year, navigate using the next-month control until the target month is displayed, and then select the required day.
121. Interview Question: How Do You Handle a Date Range Picker?
Select the start date first and the end date second. Then verify that both dates are displayed correctly and that the range follows the application's validation rules.
122. Interview Question: How Do You Handle Dynamic Date Pickers?
Use explicit waits, re-locate elements after calendar navigation, and wait for the required month or target date to become available before interacting with it.
123. Interview Question: How Do You Avoid Selecting the Wrong Day?
Do not rely only on the visible day number when duplicate dates can exist. Prefer an exact data-date attribute, accessible label, current-month class, or a calendar-scoped locator.
124. Interview Question: Why Does StaleElementReferenceException Occur?
It can occur when the calendar DOM is refreshed after navigating between months. Previously located WebElements may no longer represent the current elements, so they should be located again.
125. Interview Question: Can Select Class Be Used for Date Pickers?
The Selenium Select class can be used only with an actual HTML <select> element. A custom calendar made from divs, buttons, spans, or other elements should be handled using normal Selenium WebDriver interactions.
126. Interview Question: How Do You Verify the Selected Date?
Read the value from the date input using getAttribute("value"), or inspect selected-state attributes such as a CSS class or aria-selected.
127. Interview Question: How Do You Handle Disabled Dates?
Identify disabled dates through attributes such as disabled, CSS classes, or ARIA states and verify that the application prevents invalid selection.
128. Quick Revision
- Identify the date picker type.
- Native and custom date pickers require different strategies.
- Open the calendar before selecting a date.
- Read the displayed month and year.
- Navigate to the target month and year.
- Prefer exact date attributes when available.
- Do not depend only on day numbers when duplicate dates exist.
- Use explicit waits for dynamic calendars.
- Re-locate elements after DOM updates.
- Handle disabled and unavailable dates.
- Handle date ranges separately.
- Verify the selected date.
- Use reusable methods and Page Object Model.
129. Date Picker Testing Checklist
- Verify date field is displayed.
- Verify date field is clickable.
- Verify calendar opens.
- Verify current month and year.
- Verify next-month navigation.
- Verify previous-month navigation.
- Verify year navigation when available.
- Verify valid date selection.
- Verify disabled date behavior.
- Verify unavailable date behavior.
- Verify today's date.
- Verify date range selection.
- Verify invalid date ranges.
- Verify date format.
- Verify final input value.
- Verify calendar closing behavior.
- Verify keyboard navigation when supported.
- Verify accessibility attributes when required.
- Verify behavior after page refresh.
- Include date-picker tests in regression suites.
130. Course Resource
The Selenium Training Course covers Selenium WebDriver, locators, browser automation, web elements, waits, dynamic elements, forms, TestNG, automation frameworks, and practical testing concepts.
To enquire about the course or request a demo, visit Register for Course Demo.
131. Conclusion
Date Picker automation is an important Selenium WebDriver skill because dates are used extensively in real-world web applications. The correct automation strategy depends on how the date picker is implemented.
For a reliable test, first inspect the DOM and identify whether the control is a native HTML date input or a custom calendar. For custom calendars, open the calendar, identify the displayed month and year, navigate to the required month, locate the exact target date, select it, and verify the resulting value.
Dynamic date pickers should be synchronized using explicit waits, while calendar elements should be re-located after DOM updates to reduce stale-element failures. Stable locators such as data-date, accessible labels, IDs, and calendar-scoped selectors can make date-picker automation more maintainable.
With reusable utility methods, Java's date APIs, explicit waits, Page Object Model, proper validation, and positive and negative test scenarios, Selenium testers can automate date selection effectively across flight booking, hotel booking, appointment, banking, e-commerce, reporting, and other applications.
Selenium Training Course | Register for Course Demo