i'm trying to select Months and Dates from a calendar (www.booking.com) for practice purposes but i can not get it to select the month, if the month is into left panel. Probably i'm missing something. Can anyone give me a hint? or make an assist, it would be much appreciated. Thanks in advance.
My Code:
public void calendar() throws InterruptedException {
String selectDate = "6/11/2020";
Date d = new Date(selectDate);
SimpleDateFormat years = new SimpleDateFormat("yyyy");
SimpleDateFormat months = new SimpleDateFormat("MMMM");
SimpleDateFormat days = new SimpleDateFormat("d");
String year = years.format(d);
String month = months.format(d);
String day = days.format(d);
String gap = " ";
String search = month + gap + year;
while (!driver.findElement(By.xpath("//div[@class='xp-calendar']/div/div/div/div/*[contains(@class,'bui-calendar__month')]")).getText().equalsIgnoreCase(search)) {
Thread.sleep(1000);
driver.findElement(By.xpath("//div[@class='xp-calendar']/div/div/div[2]")).click();
}
int coutDays = driver.findElements(By.xpath("//div[@class='xp-calendar']/div/div/div/div/table/tbody/tr/td")).size();
for (int i = 0; i < coutDays; i++) {
String searchingDay = driver.findElements(By.xpath("//div[@class='xp-calendar']/div/div/div/div/table/tbody/tr/td")).get(i).getText();
if (searchingDay.equalsIgnoreCase(day)) {
Thread.sleep(1000);
driver.findElements(By.xpath("//div[@class='xp-calendar']/div/div/div/div/table/tbody/tr/td")).get(i).click();
break;
}
}
you can use the css to select the calendar date.
driver.findElement(By.cssSelector("td[data-date='2019-03-21']")).click();
Make sure you pass the date in "YYYY-MM-DD" format.
Here is the code if you want to try in your console first.
document.querySelector('td[data-date="2019-03-21"]').click()
You don't have to open the date picker, just navigate to the page and run the above.
Correct Code:
String date = "10-June 2020";
String splitter[]= date.split("-");
String checkInMonth_Year = splitter[1];
String checkInDay = splitter[0];
List<WebElement> a = driver.findElements(By.xpath("//div[@class='bui-calendar']/div/div/div/div"));
for (int i=0; i<a.size(); i++)
{
System.out.println(a.get(i).getText());
if (a.get(i).getText().equals(checkInMonth_Year))
{
List<WebElement> days = driver.findElements(By.xpath("//div[@class='bui-calendar']/div/div/div["+(i+1)+"]/table/tbody/tr/td[@class='bui-calendar__date']"));
for (WebElement d:days)
{
if (d.getText().equals(checkInDay))
{
d.click();
return;
}
}
}
}
pauseFor1Sec();
driver.findElement(By.xpath("//div[contains(@class,'bui-calendar__control bui-calendar__control--next')]")).click();
calendar();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.