priyanka srivastava priyanka srivastava - 1 month ago 6
Java Question

how to select date from calendar popup using selenium path in eclipse

I am trying to select departure date and return date from calendar pop up but finding it difficult to write a generic code for date selection .I am trying to write a method in which date is passed from main method as argument and the method will execute and click the calendar popup and select click the date.I have written the code till finding the month but after that i am stuck for date path please help.

Screen shot of pop up:

enter image description here

website I am using is click here https://www.yatra.com/

Here is my code:



package Website;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class PickDateCalender {
static WebDriver driver=new FirefoxDriver();;


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



driver.get("https://www.yatra.com/");

driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
WebElement element=driver.findElement(By.className("dropdown-toggle"));

String ori="New Delhi, India (DEL)";
String dest="Bangalore, India (BLR)";

String DepartDte="23-October-2016";
String splitter[]=DepartDte.split("-");
String Departdate=splitter[0];
System.out.println("date"+" "+Departdate);
String Departmonth=splitter[1];
System.out.println("month"+" "+Departmonth);
String Departyear=splitter[2];
System.out.println("year"+" "+Departyear);
String returDte="";
;
selectDate(Departdate,Departmonth,Departyear);

}



public static void selectDate(String Depardate,String Departmonth,String Departyear ){
WebElement Depart=driver.findElement(By.id("BE_flight_depart_date"));
Depart.click();
List <WebElement> month =driver.findElements(By.xpath(".//*[@id='PegasusCal-0']//ul[@class='month-list']"));
for(int i=0;i<month.size();i++){
String monname=month.get(i).getText();


if(monname.contains(Departmonth)){
System.out.println("Match found"+" "+monname);
System.out.println("inside if");
month.get(i).click();

break;


}
driver.close();

}
}
}

Answer

You can easily do that by a single xpath. As each date has a unique id

a_2017_3_13 which is 'a_year_month_day'

You can directly build a xpath and do it..

 private void selectDate(String Departdate, String Departmonth, String Departyear) {
        //div[@id='PegasusCal-0']//a[@id='a_2017_3_13']
        String dateXpath = String.format(
                "//div[@id='PegasusCal-0']//a[@id='a_%s_%d_%s']",
                Departyear, getMonthNum(Departmonth), Departdate);
        driver.findElement(By.xpath(dateXpath)).click();
    }

     //As you are passing input in name 'October' parsing that to number
    private int getMonthNum(String monthName) {
        try {
            Date date = new SimpleDateFormat("MMM").parse(monthName);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal.get(Calendar.MONTH) + 1;
        } catch (ParseException ex) {
            Logger.getLogger(Yatra.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 1;
    }

Full Sample

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 *
 * @author Phystem
 */
public class Yatra {

    WebDriver driver;

    public Yatra() {
        System.setProperty("webdriver.chrome.driver", "D:\\Test\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    public void start() {
        driver.get("https://www.yatra.com/");
        String DepartDte = "29-March-2017";
        String splitter[] = DepartDte.split("-");
        String Departdate = splitter[0];
        System.out.println("date" + " " + Departdate);
        String Departmonth = splitter[1];
        System.out.println("month" + " " + Departmonth);
        String Departyear = splitter[2];
        System.out.println("year" + " " + Departyear);
        String returDte = "";
        driver.findElement(By.name("flight_depart_date")).click();
        selectDate(Departdate, Departmonth, Departyear);
    }

    private void selectDate(String Departdate, String Departmonth, String Departyear) {
        //div[@id='PegasusCal-0']//a[@id='a_2017_3_13']
        String dateXpath = String.format(
                "//div[@id='PegasusCal-0']//a[@id='a_%s_%d_%s']",
                Departyear, getMonthNum(Departmonth), Departdate);
        driver.findElement(By.xpath(dateXpath)).click();
    }

    private int getMonthNum(String monthName) {
        try {
            Date date = new SimpleDateFormat("MMM").parse(monthName);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            return cal.get(Calendar.MONTH) + 1;
        } catch (ParseException ex) {
            Logger.getLogger(Yatra.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 1;
    }

    public static void main(String[] args) {
        new Yatra().start();
    }

}