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

2024-10-02 08:14:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用python2.7.8和Selenium的最新版本(我相信是2.45)。我将使用jQueryUI的datepicker示例来回答我的问题。我已成功验证当前日期是否为突出显示的日期,然后选择一个超出2天的日期(如果>;=26,则选择早于2天的日期)。在

我的问题是如何使用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)

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


Tags: theselfchilduitodaydateifis

热门问题