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, Community Contributor -

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.

Datepicker in selenium example
Datepicker on the MakeMyTrip Website

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.
  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

As always, it is important to run Selenium tests on real browsers and devices. BrowserStack offers a cloud Selenium Grid of 2000+ real browsers and devices for testing purposes. Simply sign up, choose the required device-browser-OS combination, and start testing websites for free.

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free