我正在尝试将今天的日期输入日期选择器字段,而不是单击today
按钮,下面是我的代码设置方式,但无法执行此操作。我正在使用BDD框架。
我输入了今天的日期
@When('I input todays date')
def step_impl(context):
value_paystub_dateOfPayStub(context.webdriver)
Getting element of said date picker:
def get_dateOfPaystub(driver):
element = None
try:
element = WebDriverWait(driver, 70).until(
EC.visibility_of_element_located(
(By.XPATH, "//input[@id='chl.CustomerFinancials.primaryFinancialsForm.paystub.6134.DateOfPaystub']"))
)
except TimeoutException:
logger.error("Date of Paystub element was not found")
return element
Trying to enter today's date:
def value_paystub_dateOfPayStub(driver):
currentdate = datetime.datetime.today()
try:
element = get_dateOfPaystub(driver)
element.send_keys() # to set focus on the selector
element.send_keys(currentdate, "%m-%d-%Y")
except Exception:
logger.error("Unable to enter value into Paystub Employer Name field")
任何帮助都将不胜感激。
答案 0 :(得分:0)
您需要使用strftime
格式化日期。
import datetime from datetime
element.send_keys(datetime.strftime(currentdate, '%m-%d-%Y'))
答案 1 :(得分:0)
在这里,我们需要为日历字段定义定位器,以如下所示动态地接受数据。
calendar = "//p[contains(.,'{}')]"
,然后使用下面的代码获取今天的日期并选择适当的日期
import datetime
current_day = datetime.datetime.now().day
driver.find_element_by_xpath(calendar.format(current_day).click
答案 2 :(得分:0)
这是我使用 css_selector 尝试过的:
import datetime
# Getting today's date
now = datetime.datetime.now()
tday = now.strftime("%m-%d-%Y")
# Selecting everything and clearing by backspace
elem.send_keys(Keys.CONTROL + 'a', Keys.BACKSPACE)
elem = driver.find_element_by_css_selector('#ID')
or
elem = driver.find_element_by_id('ID')
# Sending today's date
elem.send_keys(tday)
time.sleep(1)