Not able to Select dates | Selenium Forum
M
Posted on 01/06/2016
Hi Ashish,

Please i need assistance with this. I am trying to follow the select dates for calendar in the lecture video(Module 15 and Module 21 for the framework) but it seems not to be working for
me.
Please can anyone take a look for me.

In my situation my sites have different date formarts dd/MM/yyyy, dd.MM.yyyyy and yyyy-mm-ddand
i am trying to see if a common code can work for all. Below are to sets of framework. One is working
but the other is not.

example of URL with dd/mm/yyyy - https://www.americanairlines.in/intl/in/index.jsp?locale=en_IN
example of URL with dd.mm.yyyy - https://www.americanairlines.de/intl/de/index.jsp?locale=de_DE
example of URL with yyyy-mm-dd - https://www.americanairlines.jp/intl/jp/index.jsp?locale=ja_JP

1) This site has dd/MM/yyyy date formart and it works with a hard coded select day xpath instead of
the select date xpart you created.



import java.text.SimpleDateFormat;
import java.util.Calendar;
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.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;


public class HomePageTest {
WebDriver driver = null;
String date="09/11/2016";


@Test
public void BookTest(){
String browser="Mozilla";

if(browser.equals("Mozilla")){
driver=new FirefoxDriver();
}else if(browser.equals("Chrome")){
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
driver= new ChromeDriver();
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();

// Navigate and login
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();

driver.get("https://www.americanairlines.in/intl/in/index.jsp?locale=en_IN");
//https://www.americanairlines.jp/intl/jp/index.jsp?locale=ja_JP
//https://www.americanairlines.in/intl/in/index.jsp?locale=en_IN
driver.findElement(By.xpath("//*[@id='reservationFlightSearchForm.originAirport']")).sendKeys("LHR");
//iver.findElement(By.xpath("//*[@id='ui-id-1']")).sendKeys(Keys.ENTER);
driver.findElement(By.xpath("//*[@id='reservationFlightSearchForm.destinationAirport']")).sendKeys("DFW");
//iver.findElement(By.xpath("//*[@id='ui-id-2']")).sendKeys(Keys.ENTER);



// select the date.
// 1) Get the date objects
// 2) Get the month and year for date to be selected
driver.findElement(By.xpath("//*[@id='aa-leavingOn']")).click(); //click on date field

Date currentDate = new Date();// get current date
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
//dd/MM/yyyy
Date dateToBeSelected =null;
try {
dateToBeSelected = formatter.parse(date); // date object of the date to be selected
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String month=new SimpleDateFormat("MMMM").format(dateToBeSelected);
String day=new SimpleDateFormat("dd").format(dateToBeSelected);
String year=new SimpleDateFormat("yyyy").format(dateToBeSelected);
String month_yearExpected = month+" "+year;

while(true){

String month_yearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
if(month_yearDisplayed.equals(month_yearExpected))
break; // correct month

if(currentDate.after(dateToBeSelected))
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a")).click();
else
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a")).click();

}
// Select day

driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/table/tbody/tr[4]/td[3]/a")).click();
//driver.findElement(By.xpath("//a[text()='"+day+"']")).click();

//a[text()='"+day+"']
//*[@id='ui-datepicker-div']/div[1]/table/tbody/tr[4]/td[3]/a

//.click();

}

}



-------------------------------------------------------------------------------------------------------------------------------------------------

2).


import java.text.SimpleDateFormat;
import java.util.Calendar;
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.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;


public class Old_HomePageTest {
WebDriver driver = null;
String date="2016-11-09";


@Test
public void BookTest(){
String browser="Mozilla";

if(browser.equals("Mozilla")){
driver=new FirefoxDriver();
}else if(browser.equals("Chrome")){
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
driver= new ChromeDriver();
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();

// Navigate and login
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();

driver.get("https://www.americanairlines.jp/intl/jp/index.jsp?locale=ja_JP");
//https://www.americanairlines.jp/intl/jp/index.jsp?locale=ja_JP
//https://www.americanairlines.in/intl/in/index.jsp?locale=en_IN
driver.findElement(By.xpath("//*[@id='origin_display']")).sendKeys("LHR");
//iver.findElement(By.xpath("//*[@id='ui-id-1']")).sendKeys(Keys.ENTER);
driver.findElement(By.xpath("//*[@id='destination_display']")).sendKeys("DFW");
//iver.findElement(By.xpath("//*[@id='ui-id-2']")).sendKeys(Keys.ENTER);



// select the date.
// 1) Get the date objects
// 2) Get the month and year for date to be selected
driver.findElement(By.xpath("//*[@id='aa-leavingOn']")).click(); //click on date field
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Date currentDate = new Date();// get current date
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
//dd/MM/yyyy
Date dateToBeSelected =null;
try {
dateToBeSelected = formatter.parse(date); // date object of the date to be selected
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String month=new SimpleDateFormat("MM").format(dateToBeSelected);
String day=new SimpleDateFormat("dd").format(dateToBeSelected);
String year=new SimpleDateFormat("yyyy").format(dateToBeSelected);
String month_yearExpected = month+" "+year;

while(true){

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
String month_yearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a")).getText();
if(month_yearDisplayed.equals(month_yearExpected))
break; // correct month

// TODO Auto-generated catch block
e.printStackTrace();
}


if(currentDate.after(dateToBeSelected))
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a")).click();
else
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a")).click();

}
// fill company details
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/table/tbody/tr[2]/td[4]/a")).click();

//a[text()='"+day+"']
//*[@id='ui-datepicker-div']/div[1]/table/tbody/tr[4]/td[3]/a

//.click();

}

}

M
Replied on 01/06/2016

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]


M
Replied on 07/06/2016

Thanks Spport. i will try this


M
Replied on 08/06/2016

[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.


M
Replied on 09/06/2016

you can try with if else statement initialize the date differently.

Date d

if(){
double date
}else{
single date
}