使用 Python/Selenium 从日期选择器中选择日期后提取格式化日期
Extracting formatted date after selecting date from datepicker using Python/Selenium
我目前正在使用 Python 2.7.8 和最新版本的 Selenium(我相信是 2.45)。我将使用 jQuery UI 的日期选择器示例来回答我的问题。我已成功验证当前日期是突出显示的日期,然后 select 是 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)
目前我只是创建一个屏幕截图来直观地验证日期是否正确。
你只需要获取附加到日期选择器的值input
,例如:
input_element = driver.find_element_by_id('datepicker')
print input_element.get_attribute('value')
我目前正在使用 Python 2.7.8 和最新版本的 Selenium(我相信是 2.45)。我将使用 jQuery UI 的日期选择器示例来回答我的问题。我已成功验证当前日期是突出显示的日期,然后 select 是 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)
目前我只是创建一个屏幕截图来直观地验证日期是否正确。
你只需要获取附加到日期选择器的值input
,例如:
input_element = driver.find_element_by_id('datepicker')
print input_element.get_attribute('value')