Finding date link of a month using text method | Selenium Forum
M
Posted on 05/02/2017
I am trying a program to select Date from datepicker on Makemytrip.com as explained in Module 15.

But the execution stops after navigating to expected month(month to be selected) and not able to identify Date links using text() method.

In fact, my Firefox is not able to identify any link of even other webpages with text() method ([b:12mdkqf3]By.xpath("//a[text()='"+datetoSelect+"']")).click();[/b:12mdkqf3]).

Refer attachment for further help and suggest solution.

M
Replied on 06/02/2017

This is code for [b:87u6sk77][u:87u6sk77]selecting date[/u:87u6sk77][/b:87u6sk77]:

public static void selectdate(String date) throws ParseException {

SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date dateToBeSelected = df.parse(date);
Date currentDate = new Date();
d.findElement(By.xpath("//*[@id='ctl00_cnpPlaceHolder_Profile1_dpDateFrom_img']")).click();
String monthYearDisplayed = d.findElement(By.xpath("//*[@id='basicDatePickerPopUp']/table/tbody/tr/td/table[1]/tbody/tr/th")).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)) {

d.findElement(By.xpath("//td/a[text()=" + day + "]")).click();
System.out.println("found and selected");
break;

} else {
if (dateToBeSelected.after(currentDate)) {

d.findElement(By.xpath("//*[@id='basicDatePickerPopUp']/table/tbody/tr/td/table[1]/tbody/tr/td[2]/img")).click();
} else {
d.findElement(By.xpath("//*[@id='basicDatePickerPopUp']/table/tbody/tr/td/table[1]/tbody/tr/td[1]/img")).click();
}
}
monthYearDisplayed = d.findElement(By.xpath("//*[@id='basicDatePickerPopUp']/table/tbody/tr/td/table[1]/tbody/tr/th")).getText();
}
}

}


M
Replied on 06/02/2017

I updated the code as suggested in earlier reply and updated xpath of Date select. Not able to select the date from month.

Taken to last valid month in Calender (i.e. June) even when date in May month is to be selected.

Please check my code once from attachment.


M
Replied on 06/02/2017

You need to write this line with in "else" statement
[u:2axfdoys]mDisplayed= drive.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();[/u:2axfdoys]

Modified code:

SimpleDateFormat df= new SimpleDateFormat("MM/dd/yyyy");

Date dt= df.parse(date);

Date currentDate= new Date();
System.out.println(currentDate);

String monthtoSelect= new SimpleDateFormat("MMMM").format(dt);
String datetoSelect= new SimpleDateFormat("dd").format(dt);
String yeartoSelect= new SimpleDateFormat("yyyy").format(dt);

String mDisplayed= d.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();

System.out.println(mDisplayed);

String mtoSelect=monthtoSelect+" "+yeartoSelect;
System.out.println(mtoSelect);

while (true) {
if (mtoSelect.equals(mDisplayed)) {
d.findElement(By.xpath("//td/a[text()=" + datetoSelect + "]")).click();

System.out.println("Found Date");
break;
} else {

if (dt.after(currentDate)) {

d.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a/span")).click();

} else {
d.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a/span")).click();
}

}
[b:2axfdoys][u:2axfdoys]mDisplayed = d.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();[/u:2axfdoys][/b:2axfdoys]

}
}


M
Replied on 07/02/2017

Yes I corrected the code and now I am able to select any date [b:2yrsh6ni]till May month[/b:2yrsh6ni] from Calender.

My next issue is I am [b:2yrsh6ni]not able to select any date in June month[/b:2yrsh6ni] (in which only first few valid dates are clickable and other invalid dates are hidden by default.)

Shall I be able to click valid dates in last month using same code?


M
Replied on 07/02/2017

This code should work irrespective of Date/Month/Year.

As it is IRCTC site, you can select date only 4 months from current date Responsive image

you may crosscheck in any other site using this "Date" function


M
Replied on 12/02/2017

Not able to select date in last month for which few dates are disabled in IRCTC site. The problem is mostly caused because Next icon in June month is disabled and it is not able to navigate for further months.

In such cases, how to select value from right side of calender and not from left side always?

[b:3kpyp89p]For example:[/b:3kpyp89p] (Attaching screenshot for reference)
If I give date to be selected as 6th June, it still selects date from left side month(i.e. 6th May) and not from right side June month.

Responsive image

M
Replied on 12/02/2017

change the xpath of this to of right hand side date.

String mDisplayed= d.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();


M
Replied on 13/02/2017

As suggested in above posts, I am using same code for accessing date in month

String mDisplayed= d.findElement(By.xpath("//*[@id='ui-datepicker-div']/[b:2tav0y8n]div[1][/b:2tav0y8n]/div/div")).getText();

and not

String mDisplayed= d.findElement(By.xpath("//*[@id='ui-datepicker-div']/[b:2tav0y8n]div[2][/b:2tav0y8n]/div/div")).getText();

But still facing issue in selecting date of last June month. (system is stuck with May and June months open in Calender)