使用Python / Selenium从datepicker中选择日期后提取格式化日期

时间:2015-03-09 17:54:04

标签: jquery python selenium selenium-webdriver datepicker

我目前正在使用Python 2.7.8和最新版本的Selenium(我相信2.45)。我将使用jQuery UI的datepicker示例来解决我的问题。我已成功验证当前日期是突出显示的日期,然后选择2天的日期(或者如果> = 26之前)。

我的问题是如何使用Python / Selenium选择所需日期后显示的日期?我的代码如下:

def testStep4(self):
    # Verifying that the default date is today's date
    # Saves date element as integer
    dateelement1 = int(self.driver.find_element_by_css_selector("#ui-datepicker-div > table > tbody > tr:nth-child(2) > td.ui-datepicker-days-cell-over.ui-datepicker-today > a").text)

    # Saves today's date as integer
    dateelement2 = int(datetime.datetime.strftime(datetime.datetime.now(),'%d'))

    # Validates that default date is today's date
    if dateelement1 == dateelement2:
        assert(True)
    else:
        assert(False)
    # Verifies if the date is between the 1st and the 26th then adds 2 days
    if dateelement1 <= 26 and dateelement1 >= 1:
        dateelement3 = dateelement2 + 2

    # if date is greater than 26th, it subtracts 2 days
    else:
        dateelement3 = dateelement2 - 2

    # System selects the date that is either 2 days after or 2 days before from previous if statement
    if self.driver.find_element_by_css_selector('#ui-datepicker-div > table > tbody > tr:nth-child(2) > td:nth-child(4) > a').text == str(dateelement3):
        self.driver.find_element_by_css_selector('#ui-datepicker-div > table > tbody > tr:nth-child(2) > td:nth-child(4) > a').click()
        time.sleep(15)
    # System displays a screenshot of the selected date
        print "System should display the day as " + str(dateelement3) + " for the current month in screenshot"
        self.driver.save_screenshot('datepicker_selected.png')
    else:
        assert(False)

目前我只是创建一个屏幕截图,以直观地验证日期是否正确。

1 个答案:

答案 0 :(得分:1)

您只需要获取附加到datepicker input的值,例如:

input_element = driver.find_element_by_id('datepicker')
print input_element.get_attribute('value')