0

I am automating a signup form for web application and there is a date of birth field which has date-picker on it. I am sending values in it by using sendKeys and the problem is that Year is not populating while day and month are populating successfully. I have tried changing date formats (like dd-mm-yyyy,mm-dd-yyyy etc) but still no use. Please check my code given below:

{
driver.findElement(By.id("customField2")).click();
driver.findElement(By.id("customField2")).sendKeys("17072017");
}
  • 1
    Can you manually enter the same value for date of birth? There may be an allowed age range that is blocking the year value of 2017. – Kate Paulk Jul 17 '18 at 13:09
0

After I tried changing formats different ways to enter the dates and nothing I did solve my problem, I remembered that the date picker is a JS widget, so I looked up a way to use Javascript with Selenium.

I think that the Selenium methods weren't able to work with the Javascript in the date picker so the values I was entering didn't work correctly. When I changed to use the Selenium Javascript Executor, I didn't have that problem.

This is the code I found that works for the date picker.

JavascriptExecutor js = (JavascriptExecutor)driver;      
js.executeScript("document.getElementById('customField2').value='2018-07-17'");
  • If you could expand on your answer to explain why you think you needed to go to the JavascriptExecutor, then mark this as accepted, that would be good. I can think of a few reasons why you would need this, but without that information, your question and answer won't be as helpful as they could be. – Kate Paulk Jul 17 '18 at 14:55
  • Also note this is not replicating user action, and wouldn't necessarily trigger any JS behavior attached to the field. – Thomas Walpole Jul 17 '18 at 18:02
  • @KatePaulk could you please explain reasons for using this? I tried changing formats and everything but nothing solved my problem, so I just looked over and found this code which solved it. I think the main reason to use this is that datepicker is in JS so you need a JS interface to execute JS in selenium. Your help will be appreciated as I am just a beginner in automation. Thanks! – Abdullah Jul 18 '18 at 8:39
  • I've edited your answer to include the information from your comment. Feel free to edit if I misread what you were saying. – Kate Paulk Jul 18 '18 at 11:41
  • @KatePaulk thanks! that's exactly what I did! – Abdullah Jul 18 '18 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.