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)

Second Example for Date Picker in Selenium Webdriver

The second example is for www.flydubai.com

We will select 22 December 2017 for Departure, and 29 December 2017 for Return. You can find the code and my technique below. The below implementation is my own technique. You can improve it by yourself. I believe in you. :)

My Strategy:

  • Clicked the departure date.
  • Waited until the departure date table is visible.
  • Found the departure day by using CSS. It returns two days one for departure and one for return. I clicked the first one because the¬†first one is departure day.
  • Clicked the return date.
  • Waited until the return date table is visible.
  • Found the return day by using CSS. It returns two days one for departure and one for return. I clicked the second one because the¬†second one is return day.
  • Waited 5 seconds to check the operation is successfully executed.

Here is the Result:

Third Example for Date Picker in Selenium Webdriver

The third example is for Trivago.com website. Our scenario as follows:

  • Open trivago.com
  • Write “Antalya” in the search bar and wait and click the first result.
  • Wait for visibility of Hotels and DatePicker
  • Select the Start Date with CSS selector. Here we should consider both work¬†days and weekends. Thus, I used “OR” operation in CSS Selector by using the comma¬†“,” operator. It works for both work days and weekends.
  • Select the End Date with CSS selector. Here we should consider both work¬†days and weekends. Thus, I used “OR” operation in CSS Selector by using the comma¬†“,” operator. It works for both work days and weekends.
  • Select the Room Type
  • And wait for 4 seconds for results. The aim is to show how to select dates on DatePicker.

Test Code:

Selenium Webdriver Tutorial Series

Thanks.
-Onur