前言:
在web自动化测试中未免会遇到时间控制器,下面介绍三种实现方式;亲测成功;
时间控件:
第一种:通过点击和if语句联合使用
self.base_click(start_date_out)
time.sleep(2)
self.base_click(start_date_in)
time.sleep(8)
value = self.driver.find_element(By.CLASS_NAME, "ant-calendar-prev-year-btn")
value.click()
print(value)
time.sleep(1)
value.click()
print(value)
time.sleep(1)
try:
elements = self.driver.find_elements(By.CLASS_NAME, "ant-calendar-date")
time.sleep(5)
for element in elements:
if element.text == "11":
element.click()
break
except StaleElementReferenceException:
print('timeout')
第二种:通过模拟键盘
# 结束日期 def __page_endDate(self): self.base_click(end_date_out) time.sleep(1) self.base_click(end_date_in) win32api.keybd_event(17, 0, 0, 0) # ctrl键位码是17 win32api.keybd_event(65, 0, 0, 0) # a键位码是65 win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放按键 win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0) pyautogui.typewrite('2024-03-31 23:59')
第三种:点击和发送数据联合使用
# 结束日期
self.driver.find_element(By.CSS_SELECTOR, ".ant-col-6:nth-child(3) .ant-calendar-picker").click()
time.sleep(0.3)
self.driver.find_element(By.CSS_SELECTOR, ".ant-calendar-input").click()
time.sleep(0.3)
self.driver.find_element(By.CSS_SELECTOR, ".ant-calendar-input").clear()
time.sleep(0.3)
self.driver.find_element(By.CSS_SELECTOR, ".ant-calendar-input").send_keys("2021-03-31 23:59")
time.sleep(0.3)
self.driver.find_element(By.LINK_TEXT, "确 定").click()
time.sleep(0.3)