Contents

    Guides

    How to Automate Date Pickers Using Selenium

    Published on

    November 6, 2025
    How to Automate Date Pickers Using Selenium

    Date pickers are common in web forms for bookings, registrations, and schedules. Automating them with Selenium ensures date-related features like validation and range selection work accurately across browsers. However, date pickers differ widely in structure and behavior, which makes automation challenging.

    Selenium WebDriver offers several methods to automate date selection, from sending values directly to handling dynamic or JavaScript-based pickers. The best approach depends on how the element is built in the DOM and how it responds to user interaction.

    This article covers how to automate date pickers in Selenium, manage dynamic formats, and test them on real browsers and devices.

    How Selenium Handles Date Picker Elements

    Date pickers appear in two main forms: native HTML inputs and custom UI components built with libraries such as jQuery UI, React Datepicker, or Bootstrap Datepicker. Selenium interacts with each type differently, depending on how the element is rendered and controlled.

    For native HTML date inputs (<input type="date">), Selenium can send the desired date value using the sendKeys() method because the element supports direct text input. The browser automatically formats the date according to its locale, which makes this approach simple and reliable.

    Custom date pickers, however, require simulating real user interactions. Selenium must locate and click the calendar icon, switch between months or years, and select a specific day. This often involves identifying clickable cells within a table or div structure that updates dynamically when the user navigates the calendar. 

    Automating Date Selection in Selenium with Java

    Automating date selection in Selenium with Java starts by locating the date picker element and deciding whether to input a value directly or interact with the calendar widget. The method depends on how the date picker is implemented on the web page.

    For standard HTML date fields, automation is straightforward. The sendKeys() method can input a date in the required format:

    WebElement dateInput = driver.findElement(By.id("dateField"));

    dateInput.sendKeys("2025-10-28");

    For JavaScript-based or custom date pickers, automation typically involves simulating user behavior. Selenium first clicks the date picker trigger, then navigates through the calendar using locators for month, year, and day elements. 

    For example:

    driver.findElement(By.id("calendarIcon")).click();

    driver.findElement(By.xpath("//span[text()='October']")).click();

    driver.findElement(By.xpath("//td[text()='28']")).click();

    To ensure stability, use dynamic locators that rely on visible text or attributes rather than index-based XPaths. This approach adapts better to layout or structure changes in modern, responsive web applications.

    Selecting a Specific Date from a Date Picker

    Selecting a specific date requires handling the navigation logic of the calendar widget. Most date pickers display only one month at a time, so Selenium must first open the widget, then move forward or backward until the correct month and year appear before clicking the desired day.

    A reliable approach involves breaking the task into steps: open the date picker, locate the month and year elements, compare them with the target values, and navigate until they match. Example:

    driver.findElement(By.id("calendarIcon")).click();

    while (true) {

        String currentMonth = driver.findElement(By.className("ui-datepicker-month")).getText();

        String currentYear = driver.findElement(By.className("ui-datepicker-year")).getText();

        if (currentMonth.equals("October") && currentYear.equals("2025")) {

            break;

        }

        driver.findElement(By.xpath("//a[@data-handler='next']")).click();

    }

    driver.findElement(By.xpath("//a[text()='28']")).click();

    This logic ensures accurate selection even when the target date lies several months away. To make the test more resilient, replace hardcoded values with parameters or dynamically computed dates based on the current system date.

    Handling Dynamic Date Formats in Selenium

    Web applications often display dates in different formats, such as MM/dd/yyyy, dd-MM-yyyy, or yyyy/MM/dd, depending on locale or framework configuration. Selenium must align its input format with what the date picker expects; otherwise, the test may fail even when the value is correct.

    Before automating, inspect the element’s expected input format through its placeholder attribute or HTML properties. In some cases, the browser automatically converts the format, but with JavaScript-driven date pickers, Selenium must send the date in the same structure the widget validates. Example:

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");

    String date = LocalDate.of(2025, 10, 28).format(formatter);

    driver.findElement(By.id("dateInput")).sendKeys(date);

    When formats change dynamically (for example, based on user locale), fetch the format from the DOM or use test data configuration files that adapt to localization rules. This ensures that automated date inputs remain accurate across different environments and regions.

    Dealing with Disabled Dates in Date Pickers

    Many applications disable past or unavailable dates to prevent invalid selections. In such cases, Selenium must identify and skip these elements rather than attempt to click them. Disabled dates typically have distinct HTML attributes or CSS classes like disabled, ui-state-disabled, or aria-disabled="true".

    A stable automation strategy involves filtering active dates before interaction. Example:

    List<WebElement> activeDates = driver.findElements(By.xpath("//td[not(contains(@class,'disabled'))]/a"));

    for (WebElement date : activeDates) {

        if (date.getText().equals("28")) {

            date.click();

            break;

        }

    }

    This approach prevents test failures and accurately mimics real user behavior. For more complex widgets that enforce dynamic constraints (like disabling weekends or holidays), use conditional logic or business rules within the test to validate whether the date should be available for selection.

    Working with Custom Date Pickers in Selenium

    Custom date pickers are common in modern frameworks like React, Angular, and Vue. These often render elements dynamically or outside the main DOM hierarchy, which means Selenium cannot always locate them using standard locators. Interacting with such pickers requires waiting for the UI to stabilize and handling shadow DOM or dynamic rendering patterns.

    Use WebDriverWait to ensure elements are visible before interaction, and if the picker is hidden under a shadow root, access it using JavaScript execution. Example:

    WebElement shadowHost = driver.findElement(By.cssSelector("custom-datepicker"));

    SearchContext shadowRoot = shadowHost.getShadowRoot();

    shadowRoot.findElement(By.cssSelector("input")).click();

    shadowRoot.findElement(By.xpath("//div[text()='28']")).click();

    For fully custom date pickers, mapping the component’s behavior like month navigation or pop-up handling helps design robust, reusable automation utilities that can adapt to similar widgets across different applications.

    Advanced Methods for Interacting with Custom Date Pickers in Selenium

    Custom date pickers created using frameworks like React, Angular, or Vue require advanced handling because they often render asynchronously, use shadow DOMs, or respond only to JavaScript-triggered events. Selenium must therefore go beyond standard element interactions by combining JavaScript execution, waits, and adaptive locators.

    Automating JavaScript-based Date Pickers in Selenium

    JavaScript-based date pickers usually render calendars dynamically when users interact with input fields or icons. Selenium must synchronize with this behavior to avoid stale or missing element errors. Using WebDriverWait ensures the picker is fully loaded before selection:

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

    WebElement dateInput = wait.until(ExpectedConditions.elementToBeClickable(By.id("dateInput")));

    dateInput.click();

    WebElement day = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[text()='28']")));

    day.click();

    If the picker relies entirely on JavaScript for rendering, Selenium can set the value through script execution to trigger internal events:

    ((JavascriptExecutor) driver).executeScript(

        "document.getElementById('dateInput').value='2025-10-28';" +

        "document.getElementById('dateInput').dispatchEvent(new Event('change'));"

    );

    This technique ensures that all event listeners are executed, making it effective for single-page applications or dynamic UI libraries.

    Managing Dynamic Date Picker Elements in Selenium

    Dynamic date pickers often update their DOM structure when users change months, years, or focus fields. Selenium must detect and adapt to these updates instead of relying on static locators. Explicit waits, visibility checks, and conditional loops are key to handling such transitions.

    For instance, when navigating between months, the script should verify that the new month and year are rendered before proceeding:

    while (true) {

        String displayedMonth = driver.findElement(By.className("ui-datepicker-month")).getText();

        if (displayedMonth.equals("October")) break;

        driver.findElement(By.xpath("//a[@data-handler='next']")).click();

        new WebDriverWait(driver, Duration.ofSeconds(5))

            .until(ExpectedConditions.textToBePresentInElementLocated(By.className("ui-datepicker-month"), "October"));

    }

    driver.findElement(By.xpath("//a[text()='28']")).click();

    This approach prevents timing issues and ensures Selenium interacts with the correct elements even when the DOM refreshes. Managing dynamic updates effectively creates stable, reusable automation scripts that can handle complex date picker behaviors across frameworks and browsers.

    Why Run Selenium Date Picker Tests on Real Devices

    Running date picker tests on real devices ensures that calendar widgets behave consistently across browsers, operating systems, and input methods. Some date pickers rely on browser-level rendering or OS date controls, which can behave differently on Android, iOS, macOS, and Windows. Testing on real environments helps identify such inconsistencies before deployment.

    Using a cloud-based platform like BrowserStack Automate allows teams to execute Selenium date picker tests on a wide range of real browsers and devices. It ensures accurate validation of interactions like date selection, navigation, and disabled-date handling under real-world conditions. This helps confirm that the automation works reliably for all users, regardless of their device or browser setup.

    Conclusion

    Automating date pickers in Selenium requires understanding the structure and behavior of different calendar implementations. From simple HTML inputs to complex JavaScript-based widgets, effective automation depends on synchronizing with dynamic elements, managing format variations, and accurately simulating user interactions.

    Running these tests on real devices using BrowserStack Automate ensures complete coverage across browsers and platforms. Teams can verify how date pickers render, respond, and validate input in actual user conditions, enabling accurate and consistent results for every user environment.

    Run Selenium Tests on Cloud

    Data-rich bug reports loved by everyone

    Get visual proof, steps to reproduce and technical logs with one click

    Make bug reporting 50% faster and 100% less painful

    Rating LogosStars
    4.6
    |
    Category leader

    Liked the article? Spread the word

    Put your knowledge to practice

    Try Bird on your next bug - you’ll love it

    “Game changer”

    Julie, Head of QA

    star-ratingstar-ratingstar-ratingstar-ratingstar-rating

    Overall rating: 4.7/5

    Try Bird later, from your desktop