I am trying to select a particular month from the spicejet website from the “depart date” field using a while loop. When the particular month is reached, I am coming out of the while loop and then selecting a date from the particular month. In my below code, I am observing that the while loop is running in an infinite loop and not stopping when the particular month is reached.
public class CalendarHandling {
static WebDriver driver;
static By forwardArrow = By
.xpath("//*[local-name()='svg' and @data-testid='svg-img']//*[name()='g' and @transform='translate(1 1)']");
public static void main(String[] args) {
BrowserUtil butil = new BrowserUtil();
driver = butil.launchBrowser("chrome");
butil.launchURL("https://www.spicejet.com/");
selectdepartDate();
}
public static void selectdepartDate() {
//click the departure date
driver.findElement(By.xpath("(//div[@data-testid='departure-date-dropdown-label-test-id']/div/div)[2]")).click();
//store the departure month value
String departMonthValue = driver.findElement(By.xpath(
"//div[contains(@data-testid,'undefined-month')]/child::div/div[contains(normalize-space(),'June')]"))
.getText();
System.out.println(departMonthValue);
//click the forward arrow until departure month is August 2024
while (!departMonthValue.trim().contains("August 2024")) {
driver.findElement(forwardArrow).click();
departMonthValue = driver.findElement(By.xpath(
"//div[contains(@data-testid,'undefined-month')]/child::div/div[contains(normalize-space(),'July')]"))
.getText();
}
//select the date
driver.findElement(By.xpath("//div[contains(@data-testid,'undefined-month')]//div[text()='28']")).click();
}
}