Why am I getting September 23 when the condition is to get out of the loop once it finds August

30 Views Asked by At

I'm trying to automate where I'll be choosing August 23 in the date. But what is happening is that instead of August it taking another click and it lands on September. Why is that? It seems that my code is correct but I can't figure it out. Also the sysout inside the while loop can't catch up and I have no idea why as well.

WebDriver driver = new ChromeDriver();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        driver.manage().window().maximize();

        driver.get("https://www.path2usa.com/travel-companions");

        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollBy(0,800)");

        Thread.sleep(5000);
        driver.findElement(By.id("form-field-travel_comp_date")).click();

        while (!driver.findElement(By.xpath("//span[@class='cur-month']")).getText().contains("April")) {
            System.out.println("Current month: " + driver.findElement(By.xpath("//span[@class='cur-month']")).getText());
            wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='flatpickr-next-month'] //*[local-name()='svg']"))).click();
            driver.findElement(By.xpath("//span[@class='flatpickr-next-month'] //*[local-name()='svg']")).click();
        }

        List<WebElement> dates = driver.findElements(By.xpath("//span[contains(@class, 'flatpickr-day')]"));
        int count = dates.size();

        for (int i = 0; i < count; i++) {
            String text = dates.get(i).getText();
            if (text.equalsIgnoreCase("19")) {
                dates.get(i).click();
                break;
            }
        }

I tried applying explicit wait and even Thread.sleep but to no avail. Can somebody help?

0

There are 0 best solutions below