use this as a working example.
[code:2babzrgq]
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Select_date {
static WebDriver driver;
public static void main(String[] args) throws ParseException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.get("https://www.makemytrip.com");
driver.findElement(By.id("start_date_sec")).click();
selectDate("25/07/2016");
}
public static void selectDate(String date) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dateToBeSelected = df.parse(date);
Date currentDate = new Date();
String monthYearDisplayed = driver.findElement(
By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div"))
.getText();
System.out.println("month year displayed " + monthYearDisplayed);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("d").format(dateToBeSelected);
System.out.println("day is " + day);
String monthYearToBeSelected = month + " " + year;
System.out
.println("month year to be selected " + monthYearToBeSelected);
while (true) {
if (monthYearToBeSelected.equals(monthYearDisplayed)) {
driver.findElement(By.xpath("//td/a[text()=" + day + "]"))
.click();
System.out.println("found and selected");
break;
} else {
if (dateToBeSelected.after(currentDate)) {
driver.findElement(
By.xpath(".//*[@id='ui-datepicker-div']/div[2]/div/a"))
.click();
} else {
driver.findElement(
By.xpath(".//*[@id='ui-datepicker-div']/div[1]/div/a"))
.click();
}
}
monthYearDisplayed = driver.findElement(
By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div"))
.getText();
}
}
}[/code:2babzrgq]
[quote="metallisonomoruyi@yahoo.co.uk":26vk6esi]Thanks Support. i will try this[/quote:26vk6esi]
Hi,
I too faced the same issue. The actual problem is if we mention the code as
String day = new SimpleDateFormat("d").format(dateToBeSelected);
This is applicable when the String date has a single digit.
Incase the String date has double digit, we will have to provide
String day = new SimpleDateFormat("dd").format(dateToBeSelected);
Now, how do we handle both the conditions together.
you can try with if else statement initialize the date differently.
Date d
if(){
double date
}else{
single date
}