Not able to select Date from website Calendar | Selenium Forum
A
Ashik Das Posted on 28/02/2021

Hello -
Please see my code below, I'm trying to follow the project structure concept to select a date(selectDate) logic, unfortunately, this not working with my below code, I tested this in many ways with many different types of elements and compared it with "compareTO" and "equals" methods, still doesn't work. Could you please help me with this and tell me what's the issue with my code or is there something that needs to be added to the code?

Thank you, 

*****************************************************************************

package TestCases;

import java.nio.channels.Selector;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
import ProgramBase.ProgramFoundationConfig;

public class PriceLine_comWebPageTest extends ProgramFoundationConfig {

 

 

@SuppressWarnings("unlikely-arg-type")
@Test
public void AirTicketLookUp() throws InterruptedException {

driver = launchBrowser("Chrome");
log("Opening Browser");
driver.get(properties.getProperty("url"));
//waitForPagetoLoad(); not working ; error Jquery not found
driver.findElement(By.cssSelector(properties.getProperty("Flights"))).click();
driver.findElement(By.cssSelector(properties.getProperty("oneWay"))).click();
driver.findElement(By.cssSelector(properties.getProperty("depart"))).sendKeys(properties.getProperty("depart_from"));
driver.findElement(By.cssSelector(properties.getProperty("arrival"))).sendKeys(properties.getProperty("arrival_to"));



driver.manage().timeouts().implicitlyWait(05, TimeUnit.SECONDS);
//here we're selecting the passenger type and selecting number of passenger three times
driver.findElement(By.cssSelector(properties.getProperty("passenger_main"))).click();
//driver.findElement(By.cssSelector(properties.getProperty(("passenger")))).click();
for(int i =0; i<3; i++) {
driver.findElement(By.cssSelector(properties.getProperty("passenger_number"))).click();
}


driver.findElement(By.cssSelector("#traveler-selection-done-button")).click();
Thread.sleep(2000);
driver.findElement(By.cssSelector("#cabin-class-select")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement selectcabinText = driver.findElement(By.cssSelector(properties.getProperty(("cabin_class2"))));
WebElement passengerType = driver.findElement(By.cssSelector(properties.getProperty(("cabin_class2"))));
Select selectCabinClass = new Select(passengerType);
selectCabinClass.selectByVisibleText(properties.getProperty("cabin_classText"));

// here we will select the flight date
driver.findElement(By.cssSelector("#flight-date-range")).click();
System.out.println("Here is the value of currentmonthdate1: " + driver.findElement(By.xpath(properties.getProperty("currentMonthyr1"))).getText());
if(!isElementPresent(properties.getProperty("currentMonthyr1")))
failAndStopTest("Display visiting date not present");

// now select the prefer date 2
DateSelection(properties.getProperty("prefered_date_1"));

}


@SuppressWarnings({ "unlikely-arg-type", "deprecation" })
public void DateSelection(String dateValue) {

String CurrentMonthyer1 = driver.findElement(By.xpath(properties.getProperty("currentMonthyr1"))).getText();
String currnetMonthyer2= driver.findElement(By.cssSelector(properties.getProperty("currentMonthyr2"))).getText();
System.out.println("Current Display Month and Year on Cal :" + CurrentMonthyer1 +" - "+currnetMonthyer2 );
SimpleDateFormat sd = new SimpleDateFormat("MM-dd-yyyy");



try {
Date dateToBeSelected = sd.parse(dateValue);
Date TodayDisplaydate = new Date();


String day = new SimpleDateFormat("d").format(dateToBeSelected);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);

String MonthYearTobeSelected = month + " " + year;
System.out.println("Month and year to be selected " + MonthYearTobeSelected);
System.out.println("Today Display Date :" + TodayDisplaydate);
System.out.println("Date to be selected :" + dateToBeSelected );



while(!MonthYearTobeSelected.equals(CurrentMonthyer1)) {


//if above condition is true then we want to click back or forward
if(!(dateToBeSelected.equals(CurrentMonthyer1))) {

// we want to click forward
driver.findElement(By.xpath("//*[@class='CalendarCard__RightArrow-sc-1jxm5yu-3 enxWGu CalendarCard__Arrow-sc-1jxm5yu-2 bkiSIv']/*[starts-with(@class,'sc-hSdWYo dnRWNe')]")).click();
Thread.sleep(500);
System.out.println("Now Display month & year is:" +CurrentMonthyer1);
}
else if((dateToBeSelected.equals(CurrentMonthyer1))){
//we want to click on the back
//driver.findElement(By.xpath("//*[@id=\"flight-search-form\"]/div[2]/div[3]/div/div/div/div[2]/div/div/div/div[2]/div/button[1]/svg/path")).click();

//now we select the day, note extract the date with single 'd' from 'dd' other wise it will add zero front of the number
driver.findElement(By.xpath("//*[@id=\"flight-search-form\"]/div[2]/div[3]/div/div/div/div[2]/div/div/div/div[2]/div/div/div/div/div/div[4]/div[3]/div[34]/div/div/div")).click();

}
}

CurrentMonthyer1 = driver.findElement(By.cssSelector(properties.getProperty("currentMonthyr1"))).getText();
System.out.println(CurrentMonthyer1);




} catch(Exception e) {

}



}

//Is element Present
public boolean isElementPresent(String Locator) {

WebElement element =null ;

try {
element = driver.findElement(By.xpath(Locator));

}catch(Exception exception) { //if element present not found, control will come inside the exception box(catch)
//log("Exception while extracting object"+exception.getMessage());
return false;
}

//Check here to see if the element is displayed

//log("Element Visibility status :" + element.isDisplayed());
if(element.isDisplayed() == false)//if element is not displayed, then return false and continue


return false;



return true; //once the code reach to this line, then element is present and not hidden


}

//driver.quit();
}
..........................................................................................

properties file data - 

url=https://www.priceline.com/?tab=flights&vrid=5e00d5c612412c30fb67dd1b74201836
selectDate=

#In properties files, hold all the data and web site element locators and user data

#Locators
Flights=#search-tab-flights > div
oneWay=#trip-type-one-way
depart=#flight-departure-airport0
arrival=#flight-arrival-airport0
passenger_main=#traveler-selection-readonly-input > div
passenger=#traveler-selection-control-panel > div:nth-child(1) > div > span
passenger_number=#traveler-selection-adults-plus-button > div > svg
type_flight=#cabin-class-select
flight_dateSelector=#cabin-class-select
cabin_class1=#cabin-class-select > option:nth-child(1)
cabin_class2=div[class='sc-htpNat eDxLcy sc-bdVaJa kRqtmu'] > select
currentMonthyr1=//*[@class='Month__MonthName-sc-12ikvnx-0 fijJjj sc-bdVaJa kVEIUC']
currentMonthyr2=#flight-search-form > div.sc-htpNat.hctAcD.sc-bdVaJa.jPklH > div.sc-htpNat.fAPJPi.sc-bdVaJa.gTdiLm > div > div > div > div.Popover__Body-sc-1bspbh7-1.civSBv.sc-bdVaJa.gWloZo > div > div > div > div:nth-child(2) > div > div > div > div > div > div:nth-child(2) > div.Month__MonthName-sc-12ikvnx-0.fijJjj.sc-bdVaJa.kVEIUC
calendar_forward=sc-hSdWYo dnRWNe sc-fjdhpX fNpEwz
calendar_backward=//*[@id="flight-search-form"]/div[2]/div[3]/div/div/div/div[2]/div/div/div/div[2]/div/button[1]/svg/path
prefered_date_visit1=#flight-date-range=
#userData
depart_from=Boston,USA
arrival_to=Sylhet,Bangladesh
flight_date=04/12/2021
cabin_classText=Economy
prefered_date_1=06-28-2021
prefered_date_2=05-13-2021

 


A
Ashik Das Replied on 02/03/2021

Hi Ashish, I think I know what is the issue, the XPath to the calendar is not dynamic, so basically it's stuck on the same Months and it is not updating the 'CurrentMonthyer1' because the link is constant. However, in this particular website element, I found this link to have only the class attribute and no id or name attached to it. So is there any other way to make this xpath dynamic so when the next button is clicked on the calendar, it automatically gets the text for upcoming months as we click on it?

//*[@class='Month__MonthName-sc-12ikvnx-0 fijJjj sc-bdVaJa kVEIUC']

 

Thank you sir, 

Ashik

 


A
Ashish Replied on 04/03/2021

You can also take xpath from browser..


M
Replied on 06/03/2021

Good Morning, Ashish -
I've tried the xpath options few times, it's giving me the same results(see below). Also, after the code enters the while loop, it will go through the if loop only once and it exits the code with a successful compilation message. Please let me know if there is something I'm missing in my code or should I try differently?

Thank you for your support as always,
Ashik

 

 

xpath link is  >  //*[@class='Month__MonthName-sc-12ikvnx-0 fijJjj sc-bdVaJa kVEIUC']

output results: > 


Opening Browser
Here is the value of currentmonthdate1: March 2021
Current Display Month and Year on Cal :March 2021 - April 2021
Month and year to be selected June 2021
Today Display Date :Sat Mar 06 11:37:54 EST 2021
Date to be selected :Mon Jun 28 00:00:00 EDT 2021
@AfterMethod

 


A
Ashik Das Replied on 08/03/2021

Hi Ashish, 

I was able to figure out the xpath issue, it worked after spliting the link. However, the while loop is not detecting the string comparison, when the both months matches, it doesn't stop to select the day. Can you tell me what is the issue on this code please? Code is below with output results ;

..............................................................................

package TestCases;

import java.nio.channels.Selector;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
import ProgramBase.ProgramFoundationConfig;

public class PriceLine_comWebPageTest extends ProgramFoundationConfig {

 

 

@SuppressWarnings("unlikely-arg-type")
@Test
public void AirTicketLookUp() throws InterruptedException {

driver = launchBrowser("Chrome");
log("Opening Browser");
driver.get(properties.getProperty("url"));
//waitForPagetoLoad(); not working ; error Jquery not found
driver.findElement(By.cssSelector(properties.getProperty("Flights"))).click();
driver.findElement(By.cssSelector(properties.getProperty("oneWay"))).click();
driver.findElement(By.cssSelector(properties.getProperty("depart"))).sendKeys(properties.getProperty("depart_from"));
driver.findElement(By.cssSelector(properties.getProperty("arrival"))).sendKeys(properties.getProperty("arrival_to"));
driver.manage().timeouts().implicitlyWait(05, TimeUnit.SECONDS);
//here we're selecting the passenger type and selecting number of passenger three times
driver.findElement(By.cssSelector(properties.getProperty("passenger_main"))).click();
//driver.findElement(By.cssSelector(properties.getProperty(("passenger")))).click();
for(int i =0; i<3; i++) {
driver.findElement(By.cssSelector(properties.getProperty("passenger_number"))).click();
}


driver.findElement(By.cssSelector("#traveler-selection-done-button")).click();
Thread.sleep(2000);
driver.findElement(By.cssSelector("#cabin-class-select")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement selectcabinText = driver.findElement(By.cssSelector(properties.getProperty(("cabin_class2"))));
WebElement passengerType = driver.findElement(By.cssSelector(properties.getProperty(("cabin_class2"))));
Select selectCabinClass = new Select(passengerType);
selectCabinClass.selectByVisibleText(properties.getProperty("cabin_classText"));

// here we will select the flight date
driver.findElement(By.cssSelector("#flight-date-range")).click();
System.out.println("Here is the value of currentmonthdate1: " + driver.findElement(By.xpath(properties.getProperty("currentMonthyr1"))).getText());
//if(!isElementPresent(properties.getProperty("currentMonthyr1")))
//failAndStopTest("Display visiting date not present");

// now select the prefer date 2
DateSelection(properties.getProperty("prefered_date_1"));

}


@SuppressWarnings({ "unlikely-arg-type", "deprecation" })
public void DateSelection(String dateValue) {

String linkpart1 = "//*[@id='flight-search-form']/div[2]/div[3]/div/div/div/div[2]/div/div/div/div[2]/div/div/div/div/div/div[";
String linkpart2 = "]/div[1]" ;


for(int i=1; i<=11 ; i++) {

String CurrentMonthyer1 = driver.findElement(By.xpath(linkpart1 + i + linkpart2)).getText();
//String currnetMonthyer2= driver.findElement(By.cssSelector(properties.getProperty("currentMonthyr2"))).getText();
System.out.println("Current Display Month and Year on Cal :" + CurrentMonthyer1);
SimpleDateFormat sd = new SimpleDateFormat("MM-dd-yyyy");



//this logic did not work for this site, i will try another way
try {
Date dateToBeSelected = sd.parse(dateValue);
Date TodayDisplaydate = new Date();

String day = new SimpleDateFormat("d").format(dateToBeSelected);
String month = new SimpleDateFormat("MMMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);

String MonthYearTobeSelected = month + " " + year;
System.out.println("Month and year to be selected ----->"+ MonthYearTobeSelected);
System.out.println("Today Display Date ----->" + TodayDisplaydate);
System.out.println("Date to be selected ----->" + dateToBeSelected );



while(!MonthYearTobeSelected.equals(CurrentMonthyer1)) {


//if above condition is true then we want to click back or forward
if(!(dateToBeSelected.equals(CurrentMonthyer1))) {

// we want to click forward
driver.findElement(By.xpath("//*[@class='CalendarCard__RightArrow-sc-1jxm5yu-3 enxWGu CalendarCard__Arrow-sc-1jxm5yu-2 bkiSIv']/*[starts-with(@class,'sc-hSdWYo dnRWNe')]")).click();
Thread.sleep(2000);
CurrentMonthyer1 = driver.findElement(By.xpath(linkpart1 + i + linkpart2)).getText();
System.out.println(CurrentMonthyer1);
break ;

}
else if((dateToBeSelected.equals(TodayDisplaydate))){
//we want to click on the back
//driver.findElement(By.xpath("//*[@id=\"flight-search-form\"]/div[2]/div[3]/div/div/div/div[2]/div/div/div/div[2]/div/button[1]/svg/path")).click();

//now we select the day, note extract the date with single 'd' from 'dd' other wise it will add zero front of the number
driver.findElement(By.xpath("//*[@id=\"flight-search-form\"]/div[2]/div[3]/div/div/div/div[2]/div/div/div/div[2]/div/div/div/div/div/div[4]/div[3]/div[34]/div/div/div")).click();

}




}
} catch(Exception e) {

}}




}

//Is element Present
public boolean isElementPresent(String Locator) {

WebElement element =null ;

try {
element = driver.findElement(By.xpath(Locator));

}catch(Exception exception) { //if element present not found, control will come inside the exception box(catch)
//log("Exception while extracting object"+exception.getMessage());
return false;
}

//Check here to see if the element is displayed

//log("Element Visibility status :" + element.isDisplayed());
if(element.isDisplayed() == false)//if element is not displayed, then return false and continue


return false;



return true; //once the code reach to this line, then element is present and not hidden


}

//driver.quit();
}
.......................................................................

output : 

[RemoteTestNG] detected TestNG version 7.3.0
***********@BeforeMethod************
DEFAULT TEST
Reports-Sun Mar 07 14-33-44 EST 2021
D:\Selenium Training\PracticeWorkspace\ProjectFormation_DesingLayout//reports//Reports-Sun Mar 07 14-33-44 EST 2021//Screenshots
Browser is null
Starting ChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761}) on port 48370
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
[1615145625.874][WARNING]: This version of ChromeDriver has not been tested with Chrome version 88.
Mar 07, 2021 2:33:47 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C

Opening Browser
Here is the value of currentmonthdate1: March 2021
Current Display Month and Year on Cal :March 2021
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:33:58 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
March 2021
Current Display Month and Year on Cal :April 2021
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:34:00 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
April 2021
Current Display Month and Year on Cal :May 2021
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:34:03 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
May 2021
Current Display Month and Year on Cal :June 2021
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:34:05 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
Current Display Month and Year on Cal :July 2021
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:34:05 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
July 2021
Current Display Month and Year on Cal :August 2021
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:34:07 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
August 2021
Current Display Month and Year on Cal :September 2021
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:34:09 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
September 2021
Current Display Month and Year on Cal :October 2021
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:34:11 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
October 2021
Current Display Month and Year on Cal :November 2021
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:34:13 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
November 2021
Current Display Month and Year on Cal :December 2021
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:34:16 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
December 2021
Current Display Month and Year on Cal :January 2022
Month and year to be selected ----->June 2021
Today Display Date ----->Sun Mar 07 14:34:18 EST 2021
Date to be selected ----->Mon Jun 28 00:00:00 EDT 2021
@AfterMethod
PASSED: AirTicketLookUp

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

 


A
Ashish Replied on 10/03/2021

May be there is some blank space or case issue

Please do debug your program


A
Ashik Das Replied on 10/04/2021

Hi Ashish,

I couden't find any blank space on the code.Also, I tried with compareTo(), equals and is display options, noting seems to be working for this page. Any suggestions would be greatly appreciated. Thank you. 


Related Posts