Module 15 makemytrip calender | Selenium Forum
M
Posted on 27/05/2016
running same code in chrome and firefox (while loading page it was going down)gives different errors

1) In firefox giving error Exception in thread "main" org.openqa.selenium.NoSuchElementException: for clicking date in that month and year at point driver.findElement(By.xpath("//a[class='ui-state-default' and @text()='"+date+"']")).click();


in this i had to use different xpath for chorme and firefox(may be page was not loading properly)



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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class CalandermakeMyTrip {
static WebDriver driver;
public static void main(String[] args) throws ParseException {
//System.setProperty("webdriver.chrome.driver","C:\\sunita_java\\driverExe\\chromedriver.exe");
//ChromeDriver driver=new ChromeDriver();


ProfilesIni profile=new ProfilesIni();
FirefoxProfile defaultprofile=profile.getProfile("default");
driver=new FirefoxDriver(defaultprofile);


driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(15,TimeUnit.SECONDS);
driver.get("http://makemytrip.com/");

// driver.findElement(By.xpath("//*[@id='start_date_sec']/span[1]/span[1]")).click();; xpath works in chrome

driver.findElement(By.xpath(" //*[@id='start_date_sec']/span[2]")).click();;
String mydate="02/12/2016";

// String monthYearDisplayed=driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/div")).getText();
String monthYearDisplayed=driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/div")).getText();
System.out.println(monthYearDisplayed);

SimpleDateFormat df=new SimpleDateFormat("dd/MM/yyyy");
Date d=df.parse(mydate);
df=new SimpleDateFormat("MM");
String date=new SimpleDateFormat("d").format(d);
String month=new SimpleDateFormat("MMMM").format(d);
String year=new SimpleDateFormat("yyyy").format(d);
Date currentdate=new Date();
System.out.println("current date"+currentdate);

String MonthYearSelected=month+" "+year;
System.out.println("******"+MonthYearSelected);

while(true)
{
if(MonthYearSelected.equals(monthYearDisplayed))
{
System.out.println("forund correct month and year");
driver.findElement(By.xpath("//a[class='ui-state-default' and @text()='"+date+"']")).click();
break;
}else//nevegate to current month and year
{
if(d.after(currentdate))
{
// driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a/span")).click(); //works for chrome
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[3]/div/a/span")).click(); //works for firefox
}
else{
//driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/a/span")).click();//for chrome
driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a/span")).click();

}

}monthYearDisplayed=driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/div")).getText();

}

}
}

M
Replied on 28/05/2016

try to use this

[code:xkuvj7uf]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");

driver.findElement(By.id("start_date_sec")).click();
selectDate("28/05/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("MMM").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()=" + 28 + "]"))
.click();
System.out.println("found and selected");
break;

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

driver.findElement(
By.xpath("//*[@id='ui-datepicker-div']/div[3]/div/a/span"))
.click();
} else {
driver.findElement(
By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/a/span"))
.click();
}
}
monthYearDisplayed = driver.findElement(
By.xpath("//*[@id='ui-datepicker-div']/div[2]/div/div"))
.getText();
}
}

}
[/code:xkuvj7uf]