When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I will explain you how to deal with Datepickers with selenium.

I want to explain it with a scenario.

  • Go to http://www.turkishairlines.com/tr-tr/ website
  • Maximize browser
  • Get Current Day as a number
  • Get “from date“‘s date-picker. It comprises of all weeks as TR and all days as TD. It is shown as below.
  • Get all TDs because TDs are the days of the month and hold them into a WebElements list.
  • Do a for loop over all TDs and when a TD’s text is equal to current day’s number then click that element.
    (If you want you can click any day. You need to change if condition’s comparison. I gave an example into code as 18th day of the month but it is commented.)
  • Wait 4 second to see the result 🙂

DatePicker’s HTML code and all TRs and TDs:

datepicker

Here is the test code. (I used JAVA 8 and JUnit and Selenium Webdriver)