Selenium Java – How to extract data from a single dropdown (Time field)

Hello people: I have the next dude about extract data from a single calendar field.

I want to take the information (HH.MM.AM/PM) from a single date field, but I cannot do it.

I have been trying with the next code:

WebElement currTime = driver.findElement(By.xpath("/html/body/div[31]/div[2]/div/div[1]/" +
        "dv-add-activity-note-form/form/div[1]/div[2]"));

String date = ((JavascriptExecutor) driver).executeScript("return arguments[0].textContent;", currTime).toString();

log.info("Time displayed is: " + date);

The only result I got it was “Time displayed is: ” and blank

This is the part of the HTML:

<p-calendar _ngcontent-c75="" appendto="body" class="calendar-field time-field ng-tns-c28-14 ui-inputwrapper-filled ng-untouched ng-pristine ng-valid" formcontrolname="start_time" hourformat="12" placeholder="Enter time">
        <span class="ng-tns-c28-14 calendar-field-container ui-calendar ui-calendar-timeonly">
            <!---->
                <input autocomplete="off" type="text" class="ng-tns-c28-14 calendar-field-input time-input ui-inputtext ui-widget ui-state-default ui-corner-all ng-star-inserted" placeholder="Enter time"><!---->

            <!---->
        </span>
    </p-calendar>

Could anybody help me?

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest
  1. Your selector assumes some <div>
  2. Your HTML code doesn’t contain any <div>, however it contains an <input> which most probably contains the information you’re looking for

So try something like:

String date = driver.findElement(By.xpath("//input[@placeholder='Enter time']")).getAttribute("value");

this will fetch value attribute of the associated input

or even:

String date = driver.findElement(By.xpath("//p-calendar[@placeholder='Enter time']")).getAttribute("innerText");

this approach will get innerText property of the whole p-calendar tag

More information: