如何使用日期函数或将python日期放在Selenium编码下方。现在我正在将硬编码值'2021-05-01'和 2021-05-31'


element = driver.find_element_by_id("txtStarttime")
driver.execute_script("arguments[0].removeAttribute('type')", element)
new_element = driver.find_element_by_id("txtStarttime")
driver.execute_script("arguments[0].setAttribute('value','2021-05-01')", new_element)

#'value','2021-05-01'
element2 = driver.find_element_by_id("txtEndtime")
driver.execute_script("arguments[0].removeAttribute('type')", element2)
new_element2 = driver.find_element_by_id("txtEndtime")
driver.execute_script("arguments[0].setAttribute('value','2021-05-31')", new_element2)
import time
time.strftime(('%Y-%m-%d'))

输出:

'2021-06-21'

尝试relativelta

from datetime import date
from dateutil.relativedelta import relativedelta
driver = webdriver.Chrome()
today = date.today()
d = today - relativedelta(months=1)
first_day = date(d.year, d.month, 1)
first_day = first_day.strftime('%Y-%m-%d')
last_day = date(today.year, today.month, 1) - relativedelta(days=1)
last_day = last_day.strftime('%Y-%m-%d')
print(first_day)
print(last_day)

打印:

2021-05-01
2021-05-31

然后只有send_keys具有这些值:

new_element = driver.find_element_by_id("txtStarttime")
new_element.send_keys(first_day)
new_element2 = driver.find_element_by_id("txtEndtime")
new_element2.send_keys(last_day)

相关内容

  • 没有找到相关文章

最新更新