How to Select Date from Datepicker in Selenium Webdriver using Java?
Last Updated :
04 Sep, 2024
In the world of web automation, selecting a date from a datepicker is a common task, especially for applications that involve date selection, such as booking systems, forms, and reservations. Selenium WebDriver is a powerful tool for automating web browsers, and when combined with Java, it provides a robust solution for interacting with web elements, including datepickers.
This article will guide you through the steps to select a date from a datepicker using Selenium WebDriver in Java, providing a clear and practical example.
Steps to select Date from Datepicker with Selenium using Java
Here are the steps to select a date from a Datepicker using Selenium and Java:
- Find the Datepicker: Locate the Datepicker element on the webpage.
- Open the Datepicker: Click on the Datepicker to show the calendar.
- Go to the Right Month/Year: If the date you want isn’t visible, use the navigation buttons to find the correct month or year.
- Pick the Date: Click on the date you want to select.
- Check the Selection: Optionally, make sure the correct date has been picked.
Java
package com.example.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.time.Duration;
public class DatePickerExample {
public static void main(String[] args) {
// Set up ChromeDriver using WebDriverManager
WebDriverManager.chromedriver().setup();
// Initialize ChromeDriver instance
WebDriver driver = new ChromeDriver();
// Maximize the browser window
driver.manage().window().maximize();
try {
// Open the website with the Datepicker
driver.get("https://www.globalsqa.com/demo-site/datepicker/");
// Switch to the iframe containing the Datepicker
driver.switchTo().frame(driver.findElement(By.cssSelector(".demo-frame")));
// Initialize WebDriverWait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// Wait until the Datepicker element is visible and click it
WebElement datepicker = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("datepicker"))
);
datepicker.click();
System.out.println("Datepicker opened");
// Select a specific date, e.g., 27th of the current month
WebElement dateToSelect = wait.until(
ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='27']"))
);
dateToSelect.click();
System.out.println("Date '27' selected");
} finally {
// Close the WebDriver
driver.quit();
}
}
}
Output:
Date Picker Example outputHow to Handle Calendar in Selenium
Handling a calendar in Selenium often involves dealing with dynamic elements. Here are some tips to manage these elements effectively:
- Frame Handling: Many datepickers are embedded within iframes. Ensure you switch to the correct iframe before interacting with the datepicker.
- Dynamic Date Navigation: For datepickers that require navigation through months or years, locate the navigation buttons (e.g., next or previous) and use Selenium to interact with them.
- XPath and CSS Selectors: Use precise XPath or CSS selectors to locate date elements. This is crucial for selecting specific dates.
How to Run Selenium Date Picker Tests on Real Devices Using Tools
To test datepickers on real devices, you can use tools like LambdaTest or BrowserStack. These platforms offer cloud-based testing environments where you can run your Selenium tests on a wide range of real devices and browsers.
- LambdaTest: Provides a real-time testing platform for running Selenium tests on a cloud grid of real devices.
- BrowserStack: Offers access to a vast range of devices and browsers for cross-browser testing.
Why Use Tools to Automate Selenium Tests?
Automating tests with tools like LambdaTest or BrowserStack provides several advantages:
- Cross-Browser Testing: Test across multiple browsers and versions to ensure compatibility.
- Device Diversity: Access real devices for testing, which helps in identifying issues that may not appear on simulators.
- Scalability: Run tests in parallel across different environments to speed up the testing process.
Conclusion
Selecting a date from a datepicker using Selenium WebDriver and Java is a straightforward task when you follow the right steps. By leveraging Selenium's capabilities, you can automate interactions with datepickers effectively. Tools like LambdaTest and BrowserStack further enhance your testing by providing access to real devices and various browsers. With these techniques, you can ensure that your web applications handle date selection seamlessly across different platforms and devices.
Similar Reads
How to Select Value from DropDown using Java Selenium Webdriver? In web Automation testing, the common task is Selecting the Dropdowns. Selenium Webdriver mainly used for Interacting to the Web-Elements. Selecting the Web-Elements from the dropdowns is a needed for the interaction to the browser.In these guide we will going through the process of selecting the dr
2 min read
How to Click on a Hyperlink Using Java Selenium WebDriver? An open-source tool that is used to automate the browser is known as Selenium. Automation reduces human effort and makes the work comparatively easier. There are numerous circumstances in which the user wants to open a new page or perform a certain action with the click of the hyperlink. In this art
4 min read
How to Drag and Drop an Element using Selenium WebDriver in Java? Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a modern web page that has a drag and drop functionality and drag and drop is used to upload the files and so many user activities. so to perform the drag and drop actions the sel
2 min read
Selenium Webdriver Handling Checkbox using Java A set of tools and libraries that allow the user to automate interactions with web browsers is known as Selenium. It is too diverse tool that can handle every operation of a webpage starting from opening the webpage to closing of webpage. In this article, we will see the various steps to handle the
6 min read
How to get text from the alert box in java Selenium Webdriver? In Automation testing, we can capture the text from the alert message in Selenium WebDriver with the help of the Alert interface. By default, the webdriver object has control over the main page, once an alert pop-up gets generated, we have to shift the WebDriver focus from the main page to the alert
2 min read
How to select a drop-down menu value using Selenium in Python? Prerequisite: Browser Automation Using Selenium Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we will be using Python
2 min read