0

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)'
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • In `fullDate.getText().format(String.valueOf(DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm a")))` you are calling the static `String.format()` method. You may have wanted to call `LocalDate.format()` or the `format` method of some other date-time class. – Ole V.V. Sep 07 '22 at 12:03
  • Your problem seems to be that `fullDate.getText()` returns a blank string to you. Why it does that, I don’t know Selenium, so can’t tell, sorry, – Ole V.V. Sep 07 '22 at 12:25

0 Answers0