How To Handle Calendar in Selenium

In this tutorial we will learn how to handle Calendar in Selenium. I will be using findElements methods to find all the dates and then will get the text and click on desired date.

Some times there are different types of date picker are using in different web applications. So we need to first look which type of date picker you are using then use below my approach. I am using below type of date picker where month and year given in a dropdown.

How to handle calendar in selenium

How To Handle Calendar in Selenium:

Steps to handle it:-

  1. Select month and year from dropdown.
  1. Common xpath store in a string (As we know till tr xpath is same for all day). For different rows like (tr[1], tr[2] etc..) we will use loop.
  1. Apply loop for row and column to put variable as we have mentioned in point 2.
  1. Use if else condition we will check specific date.
  1. If date is matched then click and break the loop.

Follow below code as per the above steps:-

  1. Select month and year from drop down:
String date = "18-August-2018";
         String dateArr[]=date.split("-");
         String day=dateArr[0];
         String month=dateArr[1];
         String year=dateArr[2];

         // select month           
         Select sc = new Select(driver.findElement(By.name("slctMonth")));
         sc.selectByVisibleText(month);

         // select year  
         Select sct = new Select(driver.findElement(By.name("slctYear")));
         sct.selectByVisibleText(year);

2. Common xpath store in a string:

String beforexpath="//div[@id='crmcalendar']/table/tbody/tr[2]/td/table/tbody/tr[";
String afterxpath ="]/td[";

 3. Apply loop for row and column to put variable as we have mentioned in point 2:

String dayval = null;

              for (int rowNum=2; rowNum<=7; rowNum++ ){
                         
                          for(int colNum=1; colNum<=totalweekdays; colNum++){
                                      try{
                                      dayval=driver.findElement(By.xpath(beforexpath+ rowNum+ afterxpath+ colNum+"]")).getText();
                                      //System.out.println(dayval);
                          } catch(NoSuchElementException e){
                                     
                                      System.out.println("please enter a valid date");
                                      break;
                 }

4. Use if else condition to check specific date

5.  If date is matched then click and break the loop.

if(dayval.equals(day))

             {                                                                       

    driver.findElement(By.xpath(beforexpath+ rowNum+ afterxpath+ colNum+"]")).click();

           }

Full Code for Handle date picker:

package seleniumPkg;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class DatePickerTest {

            public static void main(String[] args) throws InterruptedException {

                        System.setProperty("webdriver.chrome.driver", "D:\\Selenium Jars\\chromedriver_win32\\chromedriver.exe");
                        WebDriver driver = new ChromeDriver();
                        driver.manage().window().maximize();
                        driver.manage().deleteAllCookies();
                        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
                        driver.get("https://freecrm.com");
                        driver.manage().deleteAllCookies();
                        driver.findElement(By.name("username")).sendKeys("naveenk");
                        driver.findElement(By.name("password")).sendKeys("test@123");
                        Thread.sleep(1000);
                        driver.findElement(By.xpath("//input[@type='submit']")).click();
                       
                        driver.switchTo().frame("mainpanel");

                        WebElement wb = driver.findElement(By.xpath("//ul[@class='mlddm']/li[2]/a"));
                        WebElement view_day = driver.findElement(By.xpath("//ul[@class='mlddm']/li[2]/ul/li[2]/a"));
                        Actions act = new Actions(driver);
                        act.moveToElement(wb).moveToElement(view_day).click().build().perform();//click on calendar using mouse hover

                        Thread.sleep(2000);

                        
                        String date = "18-August-2018";

                        String dateArr[] = date.split("-"); // split the date into day,month and year
                        String day = dateArr[0];
                        String month = dateArr[1];
                        String year = dateArr[2];

                        Select sc = new Select(driver.findElement(By.name("slctMonth")));
                        sc.selectByVisibleText(month);

                        Select sct = new Select(driver.findElement(By.name("slctYear")));
                        sct.selectByVisibleText(year);

                       
                        // div[@id='crmcalendar']/table/tbody/tr[2]/td/table/tbody/tr[2]/td[1]
                        // Above bold text are beforexpath and afterxpath

                        String beforexpath = "//div[@id='crmcalendar']/table/tbody/tr[2]/td/table/tbody/tr[";
                        String afterxpath = "]/td[";

                        final int totalweekdays = 7;
                        boolean flag = false;
                        String dayval = null;
                        for (int rowNum = 2; rowNum <= 7; rowNum++) {

                                    for (int colNum = 1; colNum <= totalweekdays; colNum++) {
                                                try {
                                                            dayval = driver.findElement(By.xpath(beforexpath + rowNum + afterxpath + colNum + "]")).getText();
                                                           
                                                } catch (NoSuchElementException e) {

                                                            System.out.println("please enter a valid date");
                                                            break;
                                                }
                                                System.out.println(dayval);
                                                if (dayval.equals(day)) {
                                                            driver.findElement(By.xpath(beforexpath + rowNum + afterxpath + colNum + "]")).click();
                                                            Thread.sleep(4000);
                                                            flag = true;
                                                            break;

                                                }

                                    }
                                    if (flag) {

                                                break;

                                    }

                        }

                        Thread.sleep(3000);
                        driver.quit();

            }
}

 

Don’t miss this:

How To Find Broken Links using Selenium

How to Handle Alerts in Selenium

 

 

Leave a Comment