Author - StudySection Post Views - 1,180 views
datepicker

Different Approaches to select Date from DatePicker Using Selenium

Here we are using different methods to select the date from the DatePicker using selenium java. These all methods are listed below one by one with code.

  1. Sendkeys method: In this method, a particular date is filled in the date/ calendar field with the sendkeys method.

    Code:

    public static void firstApproach() {
    // Click on the Calendar field
    WebElement sendate = driver.findElement(By.xpath("//input[@id='interview_date']"));
    // Using send method to sent particular date to the calendar field
    sendate.sendKeys("12-january-2021");
    }

  2. Selecting a particular date from DatePicker: In this method, choose a particular date using locators and perform a click action to select.

    Code:

    public static void secondApproach() {
    // Click on the Calendar field
    driver.findElement(By.xpath("//input[@id='interview_date']")).click();
    // Selecting particular date
    driver.findElement(By.xpath("//a[normalize-space()='20']")).click();
    }

  3. Getting a list of dates from the Calendar: In this method, get the list of the dates from the calendar, and iterate dates one by one.

    Code:

    public static void thirdApproach() {
    // Click on the Calendar field
    driver.findElement(By.xpath("//input[@id='interview_date']")).click();
    // Getting list of the active dates and iterating one by one
    List < WebElement > activedates = driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//a"));
    for (WebElement dates: activedates) {
    String date = dates.getText();
    System.out.println(date);
    if (date.equals("25")) {
    dates.click();
    break;
    }
    }
    }

Code with these three approaches:

import java.util.List;
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.chrome.ChromeOptions;
public class DatePicker {
public static WebDriver driver;
// Open browser
public static void openBrowser() {
// System Property for Chrome Driver
System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
// Block notification and maximize the Google Chrome Browser
ChromeOptions opts = new ChromeOptions();
opts.addArguments("--disable-notifications");
opts.addArguments("start-maximized");
driver = new ChromeDriver(opts);
// Delete all cookies
driver.manage().deleteAllCookies();
// Site URL
driver.get("https://www.studysection.com/mock-interview");
}
public static void firstApproach() {
// Click on the Calendar field
WebElement sendate = driver.findElement(By.xpath("//input[@id='interview_date']"));
// Using send method to send particular date to the date field
sendate.sendKeys("12-january-2021");
}
public static void secondApproach() {
// Click on the Calendar field
driver.findElement(By.xpath("//input[@id='interview_date']")).click();
// Selecting particular date
driver.findElement(By.xpath("//a[normalize-space()='20']")).click();
}
public static void thirdApproach() {
// Click on the Calendar field
driver.findElement(By.xpath("//input[@id='interview_date']")).click();
// Getting list of the active dates and iterating one by one
List < WebElement > activedates = driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//a"));
for (WebElement dates: activedates) {
String date = dates.getText();
System.out.println("Date available for interview: " + date);
if (date.equals("25")) {
dates.click();
break;
}
}
}
public static void main(String[] args) {
// call methods one by one
DatePicker call = new DatePicker();
call.openBrowser();
//call.firstApproach();
//call.secondApproach();
call.thirdApproach();
// Close Browser
driver.close();
}
}

DatePicker Screenshots:

Form
Data

People having good knowledge of Financial accounting can get an Accounting Certification from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial accounting or you can go for advanced level certification if you have expert level skills in Financial accounting.

Leave a Reply

Your email address will not be published.