之前对时间控件的操作使用input控件的输入基本都能解决问题,今天在写新脚本过程中,使用js对时间控件的input进行输入后提交,提交后发现接口并没有获取到我配置的时间信息:
经过各种查资料都没有解决该问题,就干脆写了一个时间控件的操作方法,操作控件:
源码:
def time_element_set(self, time_element, time_str):
"""
时间控件设置
:param time_element: 时间控件定位表达式节点路径
:param time_str: 需设置的时间,字符串,格式如:2020-07-02
:return:
"""
# 时间分割
times = time_str.split('-')
times = [int(value) for value in times]
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
chinese_list = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月']
month = ""
for index, value in enumerate(num_list):
if times[1] == value:
month = chinese_list[index]
# 先单击时间控件
self.wait_element_and_click(time_element)
# 获取当前年月
current_year = int(re.sub('\D', '', self.get_element_attribute("public.current_year", 'textContent')))
current_month = int(re.sub('\D', '', self.get_element_attribute("public.current_month", 'textContent')))
# 先判断年份
if current_year != times[0]:
year_btn = "public.previous_year" if current_year > times[0] else "public.next_year"
year_diff = abs(current_year - times[0])
for num in range(year_diff):
self.wait_element_and_click(year_btn)
time.sleep(0.2)
# 再判断月份
if current_month != times[1]:
self.wait_element_and_click("public.current_month")
self.wait_element_and_click("public.month_table", parm=[month])
time.sleep(0.2)
# 选择日期
self.wait_element_and_click("public.date_table", parm=[times[2]])
time.sleep(0.2)
说明:wait_element_and_click()、get_element_attribute()方法为我自动化框架中已经封装好的方法
wait_element_and_click():查找元素并点击
get_element_attribute():查找元素的属性
所有xpath存储在ymal文件中, 上述方法第一个参数传的就是xpath所在的yaml文件中的路径节点.
后续有时间可以详细讲讲我所写的框架