Handle jQuery Date Picker using selenium_Selenium online Training

Many applications are using jQuery Date pickers for selecting date.So selecting date picker using selenium is a not a difficult task.

In this post, I will explain how can we select date from a jQuery Date picker using  selenium webdriver.


Please find the below sample code for the same.
Sample code:
package Base;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class PickDate {
 WebDriver driver;
 String currentMonth;
 String currentYear ;
 boolean status=true;

 @BeforeClass
 public void start() throws InterruptedException{
 driver = new FirefoxDriver();
     driver.manage().window().maximize();
     driver.get("http://www.way2selenium.com/p/blog-page.html");
     Thread.sleep(5000);
 }

 @Test
 private void test() throws InterruptedException {

  //
  handleDatePicker(8,04,2015);
}

   //Reusable method for handle Date Picker
   public void handleDatePicker(int day,int month,int year) throws InterruptedException{
  
List<String> months = Arrays.asList("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

//driver.switchTo().frame(driver.findElement(By.className("demo-frame")));

  //Click on date picker
   driver.findElement(By.xpath("//input[@id='datepicker']")).click();  

  //While loop
  while(status)
  { 
      //Getting current month from calendar
  currentMonth = driver.findElement(By.className("ui-datepicker-month")).getText();
  
  //Getting current year from calendar
  currentYear = driver.findElement(By.className("ui-datepicker-year")).getText();
   
   //If current month and year same as your expected month and year
   if(months.indexOf(currentMonth)+1 == month && (year == Integer.parseInt(currentYear)))
   {
   //click on day
   driver.findElement(By.linkText(new Integer(day).toString())).click();   
   status = false;
   }
     
   //If current month and year are less than expected month and year.
   else if(months.indexOf(currentMonth)+1 < month && (year == Integer.parseInt(currentYear)) || year > Integer.parseInt(currentYear))
   {
    //Click on next button of date picker.
    driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div/a[2]/span")).click();
   }
   
  
   //If current  month and year are greater than expected month and year.
   else if(months.indexOf(currentMonth)+1 > month && (year == Integer.parseInt(currentYear)) || year < Integer.parseInt(currentYear))
   {
    //Click on previous button of date picker.
    driver.findElement(By.xpath(".//*[@id='ui-datepicker-div']/div/a[1]/span")).click();
   }
   }
 } 

}