How to Select Date from Datepicker in Selenium Webdriver using Java

Related Product On This Page Steps to select Date from Datepicker with Selenium and Java

February 05, 2026 · 8 min read · Tool Comparison
Related Product

How to Select Date from Datepicker in Selenium Webdriver using Java

Datepickers are a critical UI factor for web applications where exploiter must select appointment for bookings, scheduling, or form compliance.

Ensuring that datepickers run correctly is essential for delivering a seamless exploiter experience. Selenium WebDriver with Java is one of the most effective ways to automate datepicker testing and corroborate its demeanour across browsers.

Overview

Goal of Datepicker Automation in Selenium

To formalise that datepicker UI components allow users to select accurate date across browsers and devices, ensuring smooth user interactions for bookings, forms, and former date-driven workflow.

Key Benefits of Automating Datepickers with Selenium and Java

  • Cross-Browser Reliability
  • Real-Device Accuracy
  • Reduced Manual Effort
  • Improved Release Speed

Steps to Select Date from Datepicker in Selenium WebDriver

  1. Locate the Datepicker Element– Use browser inspect tools to identify XPath or CSS chooser.
  2. Interact with the Calendar Widget– Automate clicks to open and navigate the datepicker.
  3. Select Desired Date– Implement logic to choose the exact month, year, and day.
  4. Validate Date Selection– Use assertions to confirm that the correct date is selected.

Business Impact of Testing Datepickers with Selenium

  • Reduced risk of customer frustration from broken date fields.
  • Faster feedback rhythm and more reliable feature releases.
  • Lower cost by minimizing manual QA efforts.
  • Stronger confidence in merchandise character across platforms.

This guide explainshow to take a date from a datepicker in Selenium WebDriver using Java, with step-by-step examples on the MakeMyTrip website. By the end, you ’ ll understand how to locate datepicker elements, use XPath effectively, and run examination on real browser and devices with BrowserStack Automate for accurate, reliable consequence.

Steps to choose Date from Datepicker with Selenium and Java

  1. Find the XPath of the Datepicker component. For Chrome, right-click and inspect the given element to happen its XPath.


    Datepicker on the MakeMyTrip Website

  2. To find XPath of a UI element in Firefox, right-click on the desired element, go to “Inspect Element” to open the examiner which will help name the XPath of the desired element.


Finding XPath using Inspect Element for Google Chrome

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

Code to select a given date on the MakeMyTrip site

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 java.awt.AWTException; import java.time.Duration; import java.util.List; importee org.openqa.selenium.By; signification org.openqa.selenium.WebElement; import org.testng.Assert; signification org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class MakeMyTripDateTest {WebDriver driver; WebDriverWait await; @ BeforeMethod public vacuum openBrowser () throws InterruptedException {//Launch chrome browser and navigate to Make My Trip Site driver = new ChromeDriver (); wait = new WebDriverWait (driver, Duration.ofSeconds (10)); driver.get (`` https: //www.makemytrip.com/ ''); driver.manage () .window () .maximize (); Thread.sleep (3000);} @ Test public void tripDetails () shed InterruptedException, AWTException {// Close the modal dialog box if it appears List & lt; WebElement & gt; closeModal = driver.findElements (By.cssSelector (`` span.commonModal__close '')); if (closeModal.size () & gt; 0) {closeModal.get (0) .click ();} try {WebElement dept = wait .until (ExpectedConditions.elementToBeClickable (By.cssSelector (`` p [data-cy='departureDate '] ''))); dept.click (); Thread.sleep (2000); selectDate (`` June 2024 '', `` 8 ''); Thread.sleep (3000); String selectedDate=driver.findElement (By.xpath (`` //p [@ data-cy='departureDate '] /span [1] '')) .getText (); Assert.assertEquals (selectedDate, `` 8 '');} catch (Exception e) {e.printStackTrace ();}} public void selectDate (String month_year, String day) throws InterruptedException {// For take month and year List & lt; WebElement & gt; months = driver.findElements (By.xpath (`` //div [@ class='DayPicker-Caption '] /div '')); System.out.println (`` months numeration: `` +months.size ()); for (int i = 0; i & lt; months.size (); i++) {// Select date corresponding to the the month if (months.get (i) .getText () .equals (month_year)) {List & lt; WebElement & gt; years = driver.findElements (By.xpath (`` (//div [@ class='DayPicker-Caption '] /div) [`` + i + `` +1] / .. //following-sibling: :div [@ class='DayPicker-Body '] //div [@ class='DayPicker-Day '] //p '')); System.out.println (`` days count: `` + days.size ()); for (int j = 0; j & lt; days.size (); j++) {if (days.get (j) .getText () .equals (day)) {days.get (j) .click (); break;}}}}} @ AfterMethod public nullity closeBrowser () {driver.quit ();}}

As manifest, choose a date from a datepicker on a website is easygoing enough using. Run the code, evaluate the outcome and start applying the like process to website with this particular functionality.

How to handle Calendar in Selenium

Datepicker is the UI factor that is used to generate a Calendar in web applications. Indispensable functions in a calendar require selecting a appointment or date range. The above subdivision demonstrate how to automate calendar in Selenium using Java with the help of an instance. You can follow the same steps for calendar automation in Selenium on any website.

Also Read:

As recommended, it is important to run Selenium examination on real browsers and devices. BrowserStack offers a of 3000+ existent browser and devices for testing use. Simply choose the needed device-browser-OS combination on Dashboard, and start running Selenium Tests for free.

How to run Selenium Date Picker Tests on Real Devices utilize BrowserStack

allows you to perform Automated Cross Browser Testing across real browser, background, and mobile device, through the simple step refer below:

Step: 1- and create a free account. After successful account creation, you can view the Authentication and Security details under the Summary tab.

Step: 2-Navigate to the Capabilities Generator page to select from a comprehensive set of options.

Step: 3-Choose your desired combination of device-browser from thousands of available combinations and generate capabilities using. In the below example, Windows OS version 11 with Chrome browser version 124 are select.

Step: 4-In any java editor, make a Maven project and add Selenium Java and Java node dependencies.

& lt; dependencies & gt; & lt; colony & gt; & lt; groupId & gt; org.seleniumhq.selenium & lt; /groupId & gt; & lt; artifactId & gt; selenium-java & lt; /artifactId & gt; & lt; variation & gt; 4.5.0 & lt; /version & gt; & lt; /dependency & gt; & lt; dependency & gt; & lt; groupId & gt; org.testng & lt; /groupId & gt; & lt; artifactId & gt; testng & lt; /artifactId & gt; & lt; version & gt; 7.6.1 & lt; /version & gt; & lt; scope & gt; compile & lt; /scope & gt; & lt; /dependency & gt; & lt; /dependencies & gt;

Below broadcast showcases how to select the appointment from date picker using MakeMyTrip website on Windows OS variation 11 and on latest chrome version.

Here are the step:

  1. Launch MakeMyTrip website.
  2. Select any future date from next month from the date chooser.
  3. Assert that the escort is selected.
meaning java.net.MalformedURLException; import java.net.URL; meaning java.time.Duration; import java.util.HashMap; import java.util.List; import org.openqa.selenium.By; significance org.openqa.selenium.MutableCapabilities; importation org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.RemoteWebDriver; signification 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.BeforeTest; import org.testng.annotations.Test; public class DatePicker {public static String username = `` & lt; username & gt; ''; public static String accesskey = `` & lt; access_key & gt; ''; public static net String URL = `` https: // '' + username + ``: '' + accesskey + `` @ hub-cloud.browserstack.com/wd/hub ''; WebDriver driver; String url = `` https: //www.makemytrip.com/ ''; MutableCapabilities capabilities = new MutableCapabilities (); HashMap & lt; String, Object & gt; bstackOptions = new HashMap & lt; String, Object & gt; (); @ BeforeTest public void setUp () cast MalformedURLException {capabilities.setCapability (`` browserName '', `` Chrome ''); bstackOptions.put (`` os '', `` Windows ''); bstackOptions.put (`` osVersion '', `` 11 ''); bstackOptions.put (`` browserVersion '', `` latest ''); capabilities.setCapability (`` bstack: options '', bstackOptions); driver = new RemoteWebDriver (new URL (URL), capabilities);} @ Test public void testDatePicker () throws InterruptedException {driver.get (url); driver.manage () .window () .maximize (); Thread.sleep (5000); // Close the modal dialog box if it appears List & lt; WebElement & gt; closeModal = driver.findElements (By.cssSelector (`` span.commonModal__close '')); if (closeModal.size () & gt; 0) {closeModal.get (0) .click ();} try {WebDriverWait wait = new WebDriverWait (driver, Duration.ofSeconds (10)); WebElement dept = wait .until (ExpectedConditions.elementToBeClickable (By.cssSelector (`` p [data-cy='departureDate '] ''))); dept.click (); Thread.sleep (2000); selectDate (`` June 2024 '', `` 8 ''); Thread.sleep (3000); String selectedDate = driver.findElement (By.xpath (`` //p [@ data-cy='departureDate '] /span [1] '')) .getText (); Assert.assertEquals (selectedDate, `` 8 '');} catch (Exception e) {e.printStackTrace ();}} public void selectDate (String month_year, String day) throws InterruptedException {// For selecting month and year List & lt; WebElement & gt; months = driver.findElements (By.xpath (`` //div [@ class='DayPicker-Caption '] /div '')); System.out.println (`` month numeration: `` + months.size ()); for (int i = 0; i & lt; months.size (); i++) {// Select engagement corresponding to the the month if (months.get (i) .getText () .equals (month_year)) {List & lt; WebElement & gt; years = driver.findElements (By.xpath (`` (//div [@ class='DayPicker-Caption '] /div) [`` + i + `` +1] / .. //following-sibling: :div [@ class='DayPicker-Body '] //div [@ class='DayPicker-Day '] //p '')); System.out.println (`` years count: `` + days.size ()); for (int j = 0; j & lt; days.size (); j++) {if (days.get (j) .getText () .equals (day)) {days.get (j) .click (); break;} & amp; nbsp;}}}} @ AfterMethod public emptiness closeBrowser () {// driver.quit ();}}

After running the above programs, you can ensure the result on the page. You also get to see the execution video. You can have access to text, network, and other logs.

In short, all the environment details and desired capability are available for each run on the Automate dashboard page.

Talk to an Expert

Why use BrowserStack Automate for Selenium Tests?

Here are the reasons why you should choose BrowserStack Automate to run your Selenium Tests on Real Devices and Browsers:

  • :BrowsersStack ’ s Automate product supports parallel testing by which one can easily test their app on a pond of combination of gimmick and browser types. This ultimately aid in reduce the execution time taken to run on different platforms and device and therefore giving flying feedback of the application ’ s performance.
  • Real Devices and Browsers:It is e'er wiser to test the application on real devices and browsers to simulate a existent user experience. Testing on real devices gives the actual authority about the application performance. Functional testing and even non-functional essay such performance, load and security should be performed on real devices as testing on emulators may not give accurate resultant. With Automate one can test on any latest mobile twist or web browser without the overhead of purchasing a physical device.
  • Dedicated Dashboard:Running Selenium testcases on Automate product, make a account in a dashboard which can be refer to care and monitor the automation testing activeness. The dashboard provides an overview of the prove status as Pass/Fail/Pending with all the environment particular such as twist name, OS version, Platform, browser name, browser adaptation, try execution clip and length, screenshots, etc. It also maintains a history of test execution.
  • Custom Reports with Artifacts:In Automate, custom story can be generated to ply detailed and customized study for the automated test execution. With this feature it allows to customize the structure and substance of the study as per user ’ s need. It can include a huge range of test data such as test execution status, gimmick and browser conformation, test duration, video recording, screenshots, etc.
  • Easy Integration with CI/CD Pipeline:BrowserStack ’ s Automate product can be seamlessly incorporate with popular CI/CD tools such as Jenkins, TeamCity, TravisCI. With CI/CD in place, team can achieve faster delivery rhythm with outstanding confidence in the reliability, performance and compatibility of the coating across different devices and platforms.
Tags
91,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free