selenium时间控件的操作

      之前对时间控件的操作使用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文件中的路径节点.

后续有时间可以详细讲讲我所写的框架

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值