How to convet a datepicker value to a string with Selenium Java

133 Views Asked by At

Am using Selenium webdriver with Java so I need to print my date from date picker but its failing: I want to convert this datepicker value to a string:

//WebElement fullDate = driver.findElement(By.id("datetimepicker"));

//String date = fullDate.getText().format(String.valueOf(DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a")));

//convert the date picker value to string

public void dateExample() throws InterruptedException {
    WebDriverManager.chromedriver().setup();
    WebDriver driver = new ChromeDriver();
    String baseURL = "https://demos.telerik.com/kendo-ui/datetimepicker/index";
    driver.get(baseURL);
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    WebElement selectDate = driver.findElement(By.xpath("//button[@aria-label='Open the date view']"));
    selectDate.click();
    WebElement midButn = driver.findElement(By.xpath("//a[@title='Navigate to year view']"));
    midButn.click();

    WebElement previousBtn = driver.findElement(By.xpath("//a[@aria-label='Previous']"));
    previousBtn.click();

    WebElement nextBtn = driver.findElement(By.xpath("//a[@aria-label='Next']"));
    nextBtn.click();
    Thread.sleep(1000);
    nextBtn.click();
    Thread.sleep(1000);

    WebElement pickMonth = driver.findElement(By.xpath("//a[@aria-label='July']"));
    pickMonth.click();
    WebElement pickDay = driver.findElement(By.xpath("//a[@title='Monday, July 17, 2023']"));
    pickDay.click();
    WebElement fullDate = driver.findElement(By.id("datetimepicker"));

    String date = fullDate.getText().format(String.valueOf(DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a")));

    System.out.println("Full date is: "+fullDate.getText());//nothing is printing here

    String splitDate[] = (date.split(" ")[0].split("/"));
    System.out.println("This is the Month "+splitDate[0]);
    System.out.println("This is the day "+splitDate[1]);
    System.out.println("This is the year "+splitDate[2]);
}

Console Output:

Full date is: 
This is the Month Value(MonthOfYear,2)'
This is the day 'Value(DayOfMonth,2)'
This is the year 'Value(YearOfEra,4,19,EXCEEDS_PAD)'
0

There are 0 best solutions below