All IT Courses 50% Off
Selenium Tutorials

Selecting Date from DatePicker in Selenium Webdriver

In this article, we will discuss how to automate the Date Picker in Selenium WebDriver.

What is DatePicker?

The Datepicker is a standard form input field. To open an interactive calendar we mainly focus on the input click or use the tab key. The selected date will display in the input text box.

DatePicker Example:

  1. Launch the Clear Trip website by using the following url: https://www.cleartrip.com/
  2. Identify the Input Text box.
  3. Give the Future Date to select the Depart on.
Selecting Date from DatePicker in Selenium Webdriver

Selenium WebDriver Program

We will create our test case step by step to give you a complete understanding of how to select Date Picker in WebDriver.

Step 1: Launch the Eclipse IDE and create a New Project (DatePickerProgram)

Step 2: Right-Click on the “src” folder and create a new class from New > Class.

All IT Courses 50% Off

Give your Class name as “DatePicker” and click on the “Finish” button.

Step3. Let’s get to the coding part.

Here is the sample code to set system property for Firefox driver:

FirefoxDriver.png
  • After that, we have to initialize Firefox driver using GeckoDriver Class.

Here is the sample code to initialize Firefox driver using GeckoDriver class.

Gecko1.PNG

Combining both of the above code blocks, we will get the code snippet to launch the Firefox browser.

launch.PNG
  • After that, we need to write the code which will automate our test scenario (navigate to the desired URL).

Here is the sample code to navigate to the desired URL:

cleartrip1.PNG

The complete code till now will look something like this:

import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  
  
  
  
public class DatePicker {  
  
   public static void main(String[] args) {  
          
        // System Property for Firefox Driver   
System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe");

        // Instantiate a FirefoxDriver class.      
     WebDriver driver = new FirefoxDriver();
    
        //Launch the Website
driver.get("http://cleartrip.com");
      
    }  
  
}  

Step4.Now we will try to identify the Input Text box.

Follow the steps given below to locate the Input Text box on the sample web page.

package DatePickerProgram;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DatePicker {

public static void main(String[] args) throws InterruptedException {
String dot="10/June/2020";
String date,month,year;
String caldt,calmonth,calyear;
/*
* Spliting the String into String Array
*/
String dateArray[]= dot.split("/");
date=dateArray[0];
month=dateArray[1];
year=dateArray[2];

        // System Property for Firefox Driver   
System.setProperty("webdriver.gecko.driver","F:\\drivers\\geckodriver.exe");

        // Instantiate a FirefoxDriver class.      
     WebDriver driver = new FirefoxDriver();
    
        //Launch the Website
driver.get("http://cleartrip.com");
driver.findElement(By.id("DepartDate")).click();

WebElement cal;
cal=driver.findElement(By.className("calendar"));
calyear=driver.findElement(By.className("ui-datepicker-year")).getText();
/**
* Select the year
*/
while (!calyear.equals(year)) 
{
driver.findElement(By.className("nextMonth")).click();
calyear=driver.findElement(By.className("ui-datepicker-year")).getText();
System.out.println("The Displayed Year::" + calyear);
}

calmonth=driver.findElement(By.className("ui-datepicker-month")).getText();
/**
* Select the Month
*/
while (!calmonth.equalsIgnoreCase(month)) 
{
driver.findElement(By.className("nextMonth ")).click();
calmonth=driver.findElement(By.className("ui-datepicker-month")).getText();
}

cal=driver.findElement(By.className("calendar"));
/**
* Select the Date
*/
List<WebElement> rows,cols;
rows=cal.findElements(By.tagName("tr"));
for (int i = 1; i < rows.size(); i++) 
{
cols=rows.get(i).findElements(By.tagName("td"));
for (int k = 0; k < cols.size(); k++) 
{
caldt=cols.get(k).getText();
if (caldt.equals(date)) 
{
cols.get(k).click();
break;
}
}
}

}

}

The Output will be like as below:

output.PNG
Automation Testing
Facebook Comments

2 Comments

  1. Are you planning to attend digital marketing interviews? Worrying about how to prepare for the interview, here is the list of top Digital Marketing Interview Questions and Answers.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button