Search code examples
javaselenium-webdriverfindelement

Selenium/Java: clicking jQueryUI datepicker next arrow


I try to click to the arrow, nothing happens. I have this:

driver.findElement(By.xpath("//input[@class='ui-datepicker-inline']")).click();

or

driver.findElement(By.cssSelector("a[contains(@class, 'ui-datepicker')]")).click();

<div class="ui-datepicker-inline ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all tsDatePicker tsDatePickerFirstCalendar" style="display: block;"> 

enter image description here

I want to click to the arrow so that Mars 2025 appears


Solution

  • Looks like on the mentioned website jQueryUI datepicker is used. To click the arrow we should find how it is identified. To do it we can inspect it using DevTools:

    jQueryUI datepicker with selected next button using DevTools inspector

    It has next class: .ui-datepicker-next.ui-corner-all. So we can use it to click the arrow and the next mounth will appear - March in our case.

    Attaching the whole code snippet:

        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.WebElement;
        import org.openqa.selenium.chrome.ChromeDriver;
    
        public class SimpleSeleniumExample {
    
            public static void main(String[] args) {
                // Point to your ChromeDriver executable
                System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
    
                // Initialize ChromeDriver
                WebDriver driver = new ChromeDriver();
    
                try {
                    driver.get("https://jqueryui.com/datepicker/#inline");
    
                    takeALittleNap(); //😴
    
                    driver.switchTo()
                        .frame(driver.findElement(By.cssSelector("iframe.demo-frame")));
    
                    //click to the arrow
                    WebElement nextButton = driver.findElement(By.cssSelector(".ui-datepicker-next.ui-corner-all"));
                nextButton.click();
    
                    takeALittleNap(); //😴
    
                    String newMonth = driver.findElement(By.cssSelector(".ui-datepicker-month")).getText();
                    System.out.println("New month: " + newMonth); //should be March
    
                    takeALittleNap(); //😴
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    // Close the browser
                    driver.quit();
                }
            }
    
            private static void takeALittleNap() throws InterruptedException {
                Thread.sleep(2000);
            }
    }