Selecting date from date picker combo using common method | Selenium Forum
M
Posted on 15/09/2016
If date picker contains month & year combo, how to compare displayed month/year with expected month/year.
If month & year is displayed as text, able to compare using xpath.
but how to compare if they are populated in combo. I tried using forloop also. can you please help me in handling this scenario.
[attachment=1:2u1l989k]Date selection from combo.png[/attachment:2u1l989k]

what is expected line of code after last line (how to retrieve text of month & year after clicking on ">" icon )

M
Replied on 16/09/2016

you can get the value of selected items in drop down.


M
Replied on 16/09/2016

I am able to get values from dropdown. but question is how is it possible to compare expected month/year with displayed month/year??

what is expected line of code after last line (In While loop, if expected month/year is not equal to displayed month/year then we are comparing whether expected date is before or after current date and clicking on ">" icon. In this case, how to retrieve text of displayed month & year after clicking on ">" icon )


M
Replied on 16/09/2016

Able to fix this issue. Added below highlighted code in while loop and it worked correctly.

Responsive image

M
Replied on 16/09/2016

i am not able to understand what you are trying to do.

please try to explain what are you trying to do? then we'll move to how to do it but first please tell me what are you trying to do.


M
Replied on 18/09/2016

In this example, i tried to select date from date picker (which is combo).

As explained in video, tried to compare text visible in month & year combo with required month & year.

If month & year which are visible are not equal with required month & year then clicked on ">" icon in date picker which navigates to next month (if expected date is "greater" than current date)

If month & year which are visible are not equal with required month & year then clicked on "<" icon in date picker which navigates to next month (if expected date is "Less" than current date)

After clicking on "<" or ">" icon (based on current date), tried to read text visible in month & year combo with required month & year.

i faced difficulty in reading text visible in combo after clicking on ">" icon.


M
Replied on 19/09/2016

use this code for makemytrip


you just have to make 5 or 6 changes to the code to run on different website.

[code:2qc6d31x]package com.soapuitutorial.propertie;

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");
selectDate("25/07/2016");
}



public static void selectDate(String date) throws ParseException {

driver.findElement(By.id("start_date_sec")).click();

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:2qc6d31x]