1

I am using Selenium WebDriver. I have datepicker on a web page, and I want to set a date into that control.

I am using .SendKeys("12/31/2015"), but it does not select that date on the screen. It shows 31/__/2015 only.

So how can I set datepicker's value?

Note: Datepicker is AjaxControlToolkit:CalendarExtender

8
  • You will have to pickup date using particular date Xpath. Open calendar , Get particular day xpath , Click on that... Commented Jul 20, 2015 at 11:24
  • Date will not be fixed. it may not belong to this month. So XPath of that will not be fixed. In this case Xpath may not work. Commented Jul 20, 2015 at 11:29
  • I know that but I don't think any other option available here. you can take xpath of any date you want. It can be of any month. Commented Jul 20, 2015 at 11:31
  • I tried this. What I did is, First Click on CalendarTextBox so calendar gets open, Then click on Date's Xpath but it gives me error of SeleniumNotVisibleException. But calender is open on page and I can see that date being shown there. Commented Jul 20, 2015 at 11:55
  • Awesome , Just put explicit wait or thread.sleep(20000) after open calendar and then put your date xpath code. Actually selenium need time to find date after open calendar so. Commented Jul 20, 2015 at 11:57

2 Answers 2

4

As the Datepicker is not your code you don't need to test it's functionality, so what I would do is write a JavaScript snippet that sets it to the required value, then execute that in the browser.

2
  • It worked for me. I registered javscript like below IJavaScriptExecutor js = driver as IJavaScriptExecutor; js.ExecuteScript("$('#idofDate').click();"); it selects particular date. Commented Jul 20, 2015 at 13:22
  • (its = possessive, it's = "it is" or "it has". See for example <wikihow.com/Use-Its-and-It%27s>.) Commented Jul 20, 2015 at 21:13
2

If yout are testing mobile browser and you need type on required date field to finish fill form, my solution works with any phone in any operational system.

  JavascriptExecutor js = (JavascriptExecutor)driver;
  js.executeScript("document.getElementById('id').value='1988-01-01'");
5
  • 2
    could you elebarate on the answer a little bit? Why do you feel this is a good solution?
    – ECiurleo
    Commented Jan 13, 2016 at 7:57
  • Hi Bruno! As a general rule, when answering a question that already has an excepted answer and has for some time, you generally have to really "bring it to the table" with your answer. This isn't to discourage contributing to old posts, but the bar is set with the green checkmark. So this isn't a terrible answer, but it doesn't hold up to the answer that already exists and is accepted. Make sense?
    – corsiKa
    Commented Jan 13, 2016 at 17:04
  • @ECiurleo If yout are testing mobile browser and you need type on required date field to finish fill form, my solution works with any phone in any operational system. "corsiKa" I answering a question " I want to set a date" beacuse the acepted answer don't provide "code" to solve the problem.
    – Bruno Lee
    Commented Jan 13, 2016 at 20:35
  • add your comment details into your answer so everyone can see them. Its perfectly acceptable to improve your answers based on comment feedback, in fact, its encouraged!
    – ECiurleo
    Commented Jan 14, 2016 at 11:38
  • this is a beautiful solution, as it seems to cover any 3rd party date time picker controls as well so low level that it uses value to set any thing, I was unable to find against some particular 3rd party control and this worked like a charm Commented Nov 17, 2016 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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