How to set Datepicker/time picker

Hello, I am having troubles to set up a date on the following object:
image


I tried using set text but I am unable to add a date…

Also, I have another object is a time picker:
image


Same issue…

the code is written in React if anyone has any solution please help! thx

So date pickers can be tricky, I myself have had alot of issues with them in the past too. Now it could just be a loading issue. Not sure of your script, would be nice to see if you have a waitForElementVisible in there. For the date one i have found a key word that someone has created

@Keyword
public static void handleDatepicker(TestObject calender, String exp_Date, String exp_Month,
			String exp_Year)throws Exception{
		String expDate = null, calYear = null,datepickerText=null,minYear=null,maxYear=null;
int expMonth = 0, expYear = 0;
		WebElement datePicker;
		List<WebElement> noOfDays=null,noOfMonths=null,noOfYears=null;
boolean dateNotFound = true;
		List<String> monthList = Arrays.asList("None","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec");
def driver = DriverFactory.getWebDriver()
		WebElement SelectCalender = WebUiCommonHelper.findWebElement(calender, 20);
//		JavascriptExecutor executor = ((JavascriptExecutor)driver);
//		executor.executeScript("arguments[0].click();", SelectCalender);
SelectCalender.click()
// Set your expected date, month and year.
		expDate = (exp_Date);
		expMonth = Integer.parseInt(exp_Month);
		expYear = Integer.parseInt(exp_Year);

		WebElement datePicker_Heading1 =(driver).findElement(By.xpath("//div[@class='datepicker-days']/table/thead/tr[1]/th[2]"));
		WebDriverWait wait = new WebDriverWait(driver,10);
		wait.until(ExpectedConditions.elementToBeClickable(datePicker_Heading1));
datePicker_Heading1.click();
//executor.executeScript("arguments[0].click();", datePicker_Heading1);
		WebElement datePicker_Heading2 =(driver).findElement(By.xpath("//div[@class='datepicker-months']/table/thead/tr[1]/th[2]"));
		wait.until(ExpectedConditions.elementToBeClickable(datePicker_Heading2));
datePicker_Heading2.click();

while (dateNotFound) {
			WebElement datePicker_Heading3 =(driver).findElement(By.xpath("//div[@class='datepicker-years']/table/thead/tr[1]/th[2]"));
			wait.until(ExpectedConditions.visibilityOf(datePicker_Heading3));
datepickerText =datePicker_Heading3.getText();
			String[] datepickerYear = datepickerText.split("-");
minYear =datepickerYear[0];
maxYear = datepickerYear[1];
// If current selected month and year are same as expected month
// and year then go Inside this condition.
if((expYear >= Integer.parseInt(minYear)) && (expYear<=Integer.parseInt(maxYear)))
			{
datePicker = (driver).findElement(By.xpath("//div[@class='datepicker-years']/table"));
				noOfYears = datePicker.findElements(By.xpath("//span[contains(@class,'year')]"));
				firstloop:
for (WebElement year : noOfYears) {
// Select the date from date picker when condition
// match.
if (year.getText().equalsIgnoreCase((String)exp_Year)) {
						year.click();
						Thread.sleep(1500);
datePicker = (driver).findElement(By.xpath("//div[@class='datepicker-months']/table"));
						noOfMonths = datePicker.findElements(By.xpath("//span[@class='month']"));
						Thread.sleep(1000);
for (WebElement month : noOfMonths) {
							System.out.println(" the expected month in int  is : "+expMonth);
							System.out.println(" the expected month is : "+monthList.get(expMonth));
							System.out.println(" the Actual  month is : "+month.getText());
if ((monthList.get(expMonth)).equalsIgnoreCase(month.getText())) {
								month.click();
datePicker = (driver).findElement(By.xpath("//div[@class='datepicker-days']/table"));
								noOfDays = datePicker.findElements(By.xpath("//td[@class='day']"));
								Thread.sleep(1500);
for (WebElement cell : noOfDays) {
if (cell.getText().equalsIgnoreCase(expDate)) {
										cell.click();
break firstloop;
									}
								}
							}
						}
					}
//					else{
//					
//						throw new Exception()
//					}
				}
dateNotFound = false;
			}else if (expYear > Integer.parseInt(maxYear)) {
				WebElement Next =(driver).findElement(By.xpath("//div[@class='datepicker-years']/table/thead/tr[1]/th[@class='next']"));
if(Next.getAttribute("style").equalsIgnoreCase("visibility: visible;"))
				{// Click on next button of date picker.
					Next.click();
				}else {
throw new Exception("This is exception")
				}
			}
// If current selected month and year are greater than expected
// month and year then go Inside this condition.
else if (expYear < Integer.parseInt(minYear)) {
				WebElement Previous =(driver).findElement(By.xpath("//div[@class='datepicker-years']/table/thead/tr[1]/th[@class='prev']"));
if(Previous.getAttribute("style").equalsIgnoreCase("visibility: visible;"))
				{
// Click on previous button of date picker.
					Previous.click();
				}else{

throw new Exception("This is exception")
				}
			}
		}
	}

or you could use javascript to perform the click which is how i got my datepickers working :slight_smile:

WebElement element = WebUiCommonHelper.findWebElement(findTestObject('your/object'),30)
WebUI.executeJavaScript("arguments[0].click()", Arrays.asList(element))

If niether of these work - would be nice to see ur errors and your script

I tried to create a new Keyword, but there are so many errors on the code :disappointed_relieved: should I need to save it in a specific way? and also I tried to add the javascript and added my object but the test case won’t start because there is an issue:

unable to resolve class WebElement 

@ line 36, column 12.
WebElement element = WebUiCommonHelper.findWebElement(findTestObject(‘charter-bus-quote-wizard/input_Departure Date_ant-calendar-picker-input ant-input’),30)
^

1 error

Could you share your full script and the full error log

@Adrian_Garcia_Vargas

If I understood correctly then major problem is to select date as your date component is read-only. Objective is to set value of date which you want. I assume that it is fine if you even don’t select date from picker itself.

if my above assumption is correct -
we can achieve the goal by following steps:

  • Remove “readonly” attribute from date through automation
  • Supplied the date (Exactly same which your application do support) through automation
  • Do further operation like save, something else.

For complete solution, refer my blog here:

Hope this solve the problem! Let me know in case of any further query.

Regards,
Priyank

Uploading: datepicker.png…

hi, sorry, i want to ask as well since i have the same problem with datepicker but different UI.

I can’t select the date, month and year. and I am always have error when click button “OK”
my script is following:
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

WebUI.openBrowser(‘https://uilab.bibit.id/default/’)

WebUI.delay(1)

WebUI.click(findTestObject(‘Registration/Reg_Button’))

WebUI.click(findTestObject(‘Registration/button_tgl_ultah’))

WebUI.waitForElementNotPresent(findTestObject(‘Registration/text_thn’), 1998)

WebUI.delay(0)

WebUI.waitForElementNotPresent(findTestObject(‘Registration/text_bln’), 5)

WebUI.delay(0)

WebUI.waitForElementNotPresent(findTestObject(‘Registration/text_date’), 23)

WebUI.delay(0)

WebUI.click(findTestObject(‘Registration/button_OK_date’))

WebUI.delay(0)

WebUI.selectOptionByValue(findTestObject(‘Registration/textbox_ultah’), ‘23-05-1998’, false)

WebUI.click(findTestObject(‘Registration/next_button’))

thanks before.

I believe, In your case, Javascript executor should work. Can’t you just set the value directly through javascript and move to next?

Can you try below command

WebUI.executeJavaScript(“document.getElementsByClassName(‘am-list-content’)[0].innerHTML=‘02-02-2012’;”, null)

How to use Javascript - You can refer blog here.

Hope this works for you!

  • Priyank

thanks priyank, but it still error :frowning:
here is the error code:

05-06-2019 08:40:46 AM executeJavaScript(“document.getElementsByClassName(‘am-list-content’)[0].innerHTML=‘02-02-2012’;”, null)

Elapsed time: 0.542s

Unable to execute JavaScript. (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to execute JavaScript.
at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)

manis.applikasi I have tried to execute at my end and it seems below command is working.

WebUI.executeJavaScript('document.getElementsByClassName("am-list-content")[0].innerText="02-02-2012"', null)

Please check " better in your command.

OMG… yes, now working!!! thank you so much priyank.
i don’t know why it didn’t work in the morning, but now it works!.
i owe you one

manis.applikasi

I am glad that it works.

BTW, I have seen the site which you are automating, You may face more challenges onwards (especially in registration flow)

Few of them are -

  1. Once you set date , Next button may be disabled (Have some workaround for this)
  2. In the flow, we have SMS OTP feature. (Not sure how this can be solved)

Have a nice day. Let me know in case you struck!

Sure. I will :slight_smile:
thanks a lot

hi Priyank,
it turn out not saved in database (the date) when I check it, so i think it must select/click from dropdown.
any other option?

Date published coer implementation in Katalon studio