App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide How to Select Date from Datepicker in Selenium Webdriver using Java

How to Select Date from Datepicker in Selenium Webdriver using Java

By Garima Tiwari, Senior Content Writer -

Selenium is a widely used automation testing tool used to ensure the seamless working of web applications in accordance with predetermined technical and business requirements. Using Selenium is a great way to comply with the growing demands made upon developers and testers – faster and more efficient release of new and updated features, ideally within a few weeks.

Selenium Webdriver, a major component of the Selenium Test Suite is a web framework that runs automated tests on websites to ensure all UI elements are functioning exactly as expected.

Among different UI elements, the Date is essential for certain websites. Datepickers are often included as UI elements to websites in which the user has to select a date as an input value. This makes it an important feature that needs to be tested for accurate functioning using Selenium.

This article will explore a test case demonstrating how to select a date from a datepicker in Selenium Webdriver using Java. The example uses the MakeMyTrip website to demonstrate this function. The date to be selected is “15-AUGUST-2020” on the departure datepicker displayed on the website home page.

Steps to select Date from Datepicker with Selenium and Java

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

    Datepicker in selenium example
    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 inspector which will help identify the XPath of the desired element.

How to select date from datepicker in selenium webdriver using java
Finding XPath using Inspect Element for Google Chrome

Code to select a given date on the MakeMyTrip website

//Opening Chrome Browser
package browser;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class BrowserSelection 
{
static WebDriver driver;

public static WebDriver usingChrome()
{
System.setProperty("webdriver.chrome.driver", "E:\\SeleniumLibs\\\\chromedriver_win32\\chromedriver.exe"); 
driver = new ChromeDriver(); 
driver.manage().window().maximize();
return driver;
} 
}

//Test to select a desired date in the datepicker for departure

package makemytripdatepicker;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import browser.BrowserSelection;

public class MakeMyTripDateTest 
{
WebDriver driver;

@BeforeMethod
public void openBrowser()
{ 

driver = BrowserSelection.usingChrome(); 
}

@Test
public void tripDetails() throws InterruptedException, AWTException
{

//Modify Wait time as per the Network Ability in the Thread Sleep method

driver.get("https://www.makemytrip.com/"); 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(5000);

try
{ 

driver.findElement(By.xpath("//input[@id='hp-widget__depart']")).click();
Thread.sleep(2000);

Date d = new Date(1);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMMM-yyyy");
String date = formatter.format(d);
String splitter[] = date.split("-");
String month_year = splitter[1];
String day = splitter[0]; 
System.out.println(month_year);
System.out.println(day);


selectDate(month_year,day); 
Thread.sleep(3000);


public void selectDate(String month_year, String select_day) throws InterruptedException
{ 
List<WebElement> elements = driver.findElements(By.xpath("//div[@class='ui-datepicker-title']/span[1]"));

for (int i=0; i<elements.size();i++)
{
System.out.println(elements.get(i).getText());

//Selecting the month
if(elements.get(i).getText().equals(month_year))
{ 

//Selecting the date 
List<WebElement> days = driver.findElements(By.xpath("//div[@class='ui-datepicker-inline ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-datepicker-multi ui-datepicker-multi-2']/div[2]/table/tbody/tr/td/a"));

for (WebElement d:days)
{ 
System.out.println(d.getText());
if(d.getText().equals(select_day))
{
d.click();
Thread.sleep(10000);
return;
}
} 

} 

}
driver.findElement(By.xpath("//div[@class='ui-datepicker-inline ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-datepicker-multi ui-datepicker-multi-2']/div[2]/div/a/span")).click();
selectDate(month_year,select_day);

}

@AfterMethod
public void closeBrowser()
{
driver.quit();
}

}

As demonstrated, selecting a date from a datepicker on a website is easy enough using Selenium and Java. Run the code, evaluate the results and start applying the same process to websites with this particular functionality.

Run Selenium Tests for Free

How to handle Calendar in Selenium

Datepicker is the UI element that is used to generate a Calendar in web applications. Essential functions in a calendar require selecting a date or date range. The above section demonstrates how to automate calendar in Selenium using Java with the help of an example. You can follow the same steps for calendar automation in Selenium on any website.

As recommended, it is important to run Selenium tests on real browsers and devices. BrowserStack offers a cloud Selenium Grid of 3000+ real browsers and devices for testing purposes. Simply Sign up, choose the required device-browser-OS combination on BrowserStack Automate Dashboard, and start running Selenium Tests for free.

Run Calendar Automation in Selenium on Real Devices

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

How to find broken links in Selenium

How to handle Alerts and Popups in Selenium?

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.