Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Select Date Range From DatePicker Using Selenium Python Webdriver

When testing web applications that have datepicker with date range, we need to click on the from and to date controls to return the selected date into the datepicker's from and to fields. This article explains how to select a day from the datepicker for the "from" and "to" input box using Selenium Python Webdriver. I use the site "https://jqueryui.com/datepicker/#date-range'" for the example.

 

On the site, the datepicker date range control is within an iframe, so (1) we need to define the frame and switch the webdriver to the frame. (2) find the datepicker from element and click on it to display the calendar and find a day on the calendar and click on it  (3) find the datepicker to element and click on it to display the calendar and find a day on the calendar and click on it (4) verify that datepicker to field value equals the day we selected.

For the "From" input box, we pick "04/01/2016" , and form the "to" input box, we pick "06/01/2016". 

Step 1: Create a Python Unit Test framework file and save it as "SeleniumPythonDatePickerDateRangeUnitTest.py" and write the following scripts. 

import unittest
import time
from selenium import webdriver
from selenium.webdriver.support.select import Select
class DatePickerDateRangeTest(unittest.TestCase):

    def setUp(self):
        #define a driver instance, for example Chrome
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
    def test_date_picker_date_range_(self):
        driver = self.driver
        #navigate to the test website
        driver.get('https://jqueryui.com/datepicker/#date-range')
        time.sleep(3)
        #define frame
        frame=driver.find_element_by_tag_name('iframe')
        #switch to frame
        driver.switch_to_frame(frame)
        #Choose From Month Day Date
        datepicker_from=driver.find_element_by_xpath("//input[@id='from']")
        datepicker_from.click()
        time.sleep(2)
        month_from=driver.find_element_by_xpath("//div/select[@class='ui-datepicker-month']")
        select_from_month=Select(month_from)
        select_from_month.select_by_visible_text("Apr")
        time.sleep(2)
        day_from=driver.find_element_by_xpath("//table/tbody/tr/td/a[text()='1']")
        day_from.click()
        time.sleep(5)
        #Choose To Month Day Date
        datepicker_to=driver.find_element_by_xpath("//input[@id='to']")
        datepicker_to.click()
        time.sleep(2)
        month_to=driver.find_element_by_xpath("//div/select[@class='ui-datepicker-month']")
        select_to_month=Select(month_to)
        select_to_month.select_by_visible_text("Jun")
        time.sleep(2)
        day_to=driver.find_element_by_xpath("//table/tbody/tr/td/a[text()='1']")
        day_to.click()
        time.sleep(5)
        #Get the string in To input
        to_month_string=datepicker_to.get_attribute('value')
        print to_month_string
        self.assertEqual(to_month_string,'06/01/2016')

    def tearDown(self):
        self.driver.close()
        self.driver.quit()
        
if __name__ == "__main__":
    unittest.main()

Execute the script with the command line below and observe the test result on the console. The test passed.

C:\Python27>python SeleniumPythonDatePickerDateRangeUnitTest.py
06/01/2016
.
----------------------------------------------------------------------
Ran 1 test in 27.519s

OK

C:\Python27>