http://stackoverflow.com/questions/19637507/java-webdriver-element-not-visible-exception
My code is like yours but it does not work. Can you please check and see whats wrong?
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 Calen_Makemytrip {
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("23/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[2]/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("//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[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();
}
}
}
Im getting below error which means it can not locate the forward button in the calendar but I don't understand why the code even goes there because the month and year I selected is equal to the month
and year displayed. Can you please run my code and see what you get?
your xpath is is not working.
selenium is not able to find the element.
please check the xpath.
Can you please run your code from your end to see if its still working for you? My questions is why does it even
go to the xpath its complaining because the date I selected is current.
xpath was wrong, it was pointing at right side of calender. now it is in left side.
[code:30bxfue1]String monthYearDisplayed = driver.findElement(
By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div"))
.getText();
[/code:30bxfue1]
That is not the only issue. There are several problems in this code. The xpath for the date is not working either. My questions are:
1. Does your code still work for you?
2. Why does it even try to click the next button when the month and year to be selected is the same as the month and year displayed? I dont understand.
3. Can you print out the text of the date you want to select from your xpath? String text = driver.findElement(By.xpath("//a[text()='"+day+"']")).getText();
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 Calendar_Trip {
static WebDriver driver;
public static void main(String[] args) throws ParseException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.get("http://www.makemytrip.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id='start_date_sec']/span[1]")).click();
selectDate("23/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 is "+monthYearDisplayed);
String month = new SimpleDateFormat("MMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("d").format(dateToBeSelected);
String monthYearToBeSelected = month+""+ year;
System.out.println("month year to be selected is "+monthYearToBeSelected);
String text = driver.findElement(By.xpath("//a[text()='"+day+"']")).getText();
System.out.println("day is "+text);
while(true){
if(monthYearToBeSelected.equals(monthYearDisplayed)){
driver.findElement(By.xpath("//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[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[1]/div/div")).getText();
}
}
}
I think it is problem with location problem, when try to access and print "current date", there is mismatch in date and
time. Set to IST timings in your PC and try.
I faced the same problem, month name was not giving expected results., i used http://us.makemytrip.com
[quote:1ji0jdl1]1. Does your code still work for you?
2. Why does it even try to click the next button when the month and year to be selected is the same as the month and year displayed? I dont understand.
3. Can you print out the text of the date you want to select from your xpath? String text = driver.findElement(By.xpath("//a[text()='"+day+"']")).getText();
[/quote:1ji0jdl1]
3) this xpath is also wrong.
[color=#FF0000:1ji0jdl1]"//a[text()='"+day+"']"[/color:1ji0jdl1]
please watch video about how to make partial xpaths.
2) your xpath was wrong.
[attachment=0:1ji0jdl1]Capture.PNG[/attachment:1ji0jdl1]
Can you click date with your code? driver.findElement(By.xpath("//a[text()='"+day+"']")).click();
This is your code and it does not work.
xpath was wrong use this one
[quote:3it4w9qw]driver.findElement(By.xpath("//td/a[text()=" + 28 + "]"))
.click();
[/quote:3it4w9qw]
Thanks, the day xpath is working now. Another issue im facing is the loop. If I want to select the next month, it keeps clicking the next > button until it can't click any more.
it does not stop and click. Does your code work when you select december 20, 2016?
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 Calendar_Trip {
static WebDriver driver;
public static void main(String[] args) throws ParseException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.get("http://www.makemytrip.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id='start_date_sec']/span[1]")).click();
selectDate("29/06/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 is "+monthYearDisplayed);
String month = new SimpleDateFormat("MMM").format(dateToBeSelected);
String year = new SimpleDateFormat("yyyy").format(dateToBeSelected);
String day = new SimpleDateFormat("d").format(dateToBeSelected);
String monthYearToBeSelected = month+" "+ year;
System.out.println("month year to be selected is "+monthYearToBeSelected);
String text = driver.findElement(By.xpath("//td/a[text()="+day+"]")).getText();
System.out.println("day is "+text);
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[3]/div/a/span")).click();
driver.findElement(By.xpath("//a/span[@class='ui-icon ui-icon-circle-triangle-e']")).click();
}else{
driver.findElement(By.xpath("//a/span[@class='ui-icon ui-icon-circle-triangle-w']")).click();
}
monthYearDisplayed = driver.findElement(By.xpath("//*[@id='ui-datepicker-div']/div[1]/div/div")).getText();
}
}
}
}
Yes i watched the video and my code is exactly the same as yours. How do you make it stop and click on the day when the month and year displayed is the same as the month
and year selected? It keeps clicking next until the end for me.
[code:35jfr8vv]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("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:35jfr8vv]
does the code work for you? its still clicking the next button until it can't click any more.