from selenium import webdriver
import time
from datetime import datetime
driver = webdriver.Chrome(executable_path ="C:/ChromeDriver/chromedriver.exe")
driver.maximize_window()
driver.get("http://www.timestampconvert.com")
driver.implicitly_wait(2)
time.sleep(2)
date_displayed = driver.find_element_by_xpath('/html/body/div[4]/div[2]/form/div/div[3]/div/span[5]')
print('Date displayed on page:', date_displayed.text)
try:
datetime.strptime(date_displayed.text, '%d-%m-%Y %H:%M:%S')
print('The date format {} is valid.'.format(date_displayed.text))
except ValueError:
print('The date {} is invalid'.format(date_displayed.text))
上面的代码执行得很好,但它并没有做我所期望的事情1.我只是想显示日期和剥离时间,但它同时显示日期和时间?我只是想显示日期和时间,但它同时显示日期和时间? 2.我如何验证日期和月份?
例如如果日期显示为24052020 - 它应该通过,因为预期的日期格式是DD-MM-YYY如果日期显示为06162020 - 它应该失败,因为预期的日期格式是DD-MM-YYY,但实际显示的格式是MMDDYYY。
--我有一个要求,在我的应用程序上测试特定的日期格式,我想指定第一个数字总是'DD',它的范围从1到31,第二个数字总是'MM',它的范围从1到12。
它应该验证,并应相应地通过失败的测试。
有什么建议吗?
为什么不
try:
mydate=date_display.test.split(" ")[0]
datetime.strptime(mydate, '%d-%m-%Y')
print('The date format {} is valid.'.format(mydate))
except ValueError:
print('The date {} is invalid'.format(mydate))