• UI自动化


    UI自动化写脚本【自用】

    定位方式

    1、id
    2、name
    3、class_name
    4、tag_name
    5、link_text
    6、partial_link_text
    7、XPath
    8、CSS
    通过属性定位元素: find_element_by_xpath(“//标签名[@属性=‘属性值’]”)

    id属性

    find_element_by_xpath("//input[@id=‘’]")
    
    • 1

    class属性

    find_element_by_xpath("//input[@class=‘’]")
    
    • 1

    name属性

    find__element_by_xpath("//input[@name=‘’]")
    
    • 1

    常用操作

    (1)点击操作click():

            self.driver.find_element_by_xpath(xpath).click()
            
            self.driver.find_element_by_id(xpath).click()
            
            self.driver.find_element_by_class_name(xpath).click()
            
            self.driver.find_element_by_css_selector(xpath).click()
            
            self.driver.find_elements_by_xpath(xpath)[number].click()
            self.driver.find_elements_by_css_selector(xpath)[number].click()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述
    在这里插入图片描述

    (2)输入内容send_keys():

            self.driver.find_element_by_xpath(xpath).send_keys(keys)
            
            self.driver.find_element_by_id(xpath).send_keys(keys)
            
            self.driver.find_element_by_class_name(xpath).send_keys(keys)
            
            self.driver.find_element_by_css_selector(xpath).send_keys(keys)
            
            self.driver.find_elements_by_xpath(xpath)[number].send_keys(keys)
            self.driver.find_elements_by_css_selector(xpath)[number].send_keys(keys)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    (3)清除clears():

            self.driver.find_element_by_xpath(xpath).clear()
    
            self.driver.find_element_by_id(xpath).clear()
    
            self.driver.find_element_by_class_name(xpath).clear()
    
            self.driver.find_element_by_css_selector(xpath).clear()
            
            self.driver.find_elements_by_xpath(xpath)[number].clear()
            self.driver.find_elements_by_css_selector(xpath)[number].clear()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述
    在这里插入图片描述
    (4)下拉选择drop():

            self.driver.find_element_by_xpath(xpath).click()
            sleep(1)
            self.driver.find_element_by_xpath(xpath1).click()
    
            self.driver.find_element_by_id(xpath).click()
            sleep(1)
            self.driver.find_element_by_id(xpath1).click()
    
            self.driver.find_element_by_class_name(xpath).click()
            sleep(1)
            self.driver.find_element_by_class_name(xpath1).click()
    
            self.driver.find_element_by_css_selector(xpath).click()
            sleep(1)
            self.driver.find_element_by_css_selector(xpath1).click()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述
    在这里插入图片描述
    1.下拉选择
    self.driver.find_element_by_xpath(‘//[@x-placement=“bottom-start”]/div/div/ul/li[1]‘).click()
    2.上拉选择
    self.driver.find_element_by_xpath(’//
    [@x-placement=“top-start”]/div/div/ul/li[1]’).click()

    (5)参数对比:

        ""参数对比check"""
        text_data = self.driver.find_element_by_xpath(xpath).text
        print(text_data)
        assert text == text_data
        
        ""参数对比check_error"""
        text_data = self.driver.find_element_by_xpath(xpath).text
        print(text_data)
        assert text != text_data
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (6)悬浮按钮操作choice_xf):

            ac = ActionChains(self.driver)  # 获取实例化ActionChains类
            act = self.driver.find_element_by_xpath(xpath)
            ac.move_to_element(act).perform()
            sleep(1)
            self.driver.find_element_by_xpath(xpath1).click()
        if mold == 'css':
            ac = ActionChains(self.driver)  # 获取实例化ActionChains类
            act = self.driver.find_element_by_css_selector(xpath)
            ac.move_to_element(act).perform()
            sleep(1)
            self.driver.find_element_by_css_selector(xpath1).click()
        
        choices_xf():
            ac = ActionChains(self.driver)  # 获取实例化ActionChains类
            act = self.driver.find_element_by_xpath(xpath)
            ac.move_to_element(act).perform()
            sleep(1)
            self.driver.find_elements_by_xpath(xpath1)[number].click()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    (7)元素按tab键:

            self.driver.find_element_by_xpath(xpath).send_keys(Keys.TAB)
        if mold == 'css':
            self.driver.find_element_by_css_selector(xpath).send_keys(Keys.TAB)
    
    • 1
    • 2
    • 3

    (8)按住元素进行拖动:

        a = self.driver.find_element_by_xpath(xpath)
        ac = ActionChains(self.driver)
        ac.drag_and_drop_by_offset(a, x, y).perform()
    
    • 1
    • 2
    • 3

    (9)获取当天日期:

         self.driver.find_element_by_xpath("//input[@placeholder='请输入 名称']").send_keys(str(datetime.date.today()))
    
         self.driver.find_elements_by_xpath("//input[@placeholder='请输入 名称']")[1].send_keys(str(datetime.date.today()))
         
        if many is None:
            self.driver.find_elements_by_css_selector(xpath)[number].send_keys(str(datetime.date.today()))
        else:
            self.driver.find_elements_by_css_selector(xpath)[number].send_keys(str(datetime.date.today()))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    (10)获取明天日期:

         tomorrow = str(datetime.date.today() + datetime.timedelta(days=1))
         self.driver.find_element_by_xpath("//input[@placeholder='请输入 名称']").send_keys(tomorrow)
    
    • 1
    • 2

    (11)获取日期:

         # 获取日期xxxx-xx-xx格式
         self.send_keys('[placeholder="%s"]' % text, can,  'css')
    
    • 1
    • 2

    (12)上传文件:

         self.driver.find_elements_by_xpath('//*[@class="el-upload__input"]')[number].send_keys(keys)
    
    • 1

    (13)验证列表数据:

        text_data = self.driver.find_element_by_xpath(xpath).text
        print(text_data)
        assert text == text_data
    
    • 1
    • 2
    • 3

    (13)选择标签按钮:

        if number is None:
            self.driver.find_element_by_xpath('//*[@content="请输入"]/div/div/label[1]').click()
        else:
            self.driver.find_elements_by_xpath('//*[@content="请输入"]/div/div/label[1]')[number].click()
    
    • 1
    • 2
    • 3
    • 4

    (13)获取验证码:

        self.driver.find_element_by_css_selector('[placeholder="请输入搜索内容"]').send_keys("短信发送记录")
        sleep(1)
        self.driver.find_elements_by_xpath('//*[contains(text(),"短信发送记录")]')[number].click()
        text_data = self.driver.find_element_by_xpath('//*[@id="avue-view"]/div/div/div/div[2]/div/form/div/div[3]/table/tbody/tr[1]/td[5]').text
        s = re.findall("\d+", text_data)[0]  # 正则表达式提取验证码
        print(s)
        return s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    (14)拖到目录滚动条:

        self.driver.set_window_size(1600, 900)
        self.driver.maximize_window()
        sleep(1)
        a = self.driver.find_element_by_xpath('//*[@class="avue-left"]/div/div[2]/div[3]/div')
        ac = ActionChains(self.driver)
        ac.drag_and_drop_by_offset(a, x, y).perform()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (15)下拉选择,多数据被遮挡情况:

        if many is None:
            self.driver.find_element_by_css_selector('//*[@[placeholder="%s"]').click()
            sleep(0.5)
            trialYear = self.find_xpath('//*[@x-placement="bottom-start"]/div/div/ul/li[%d]' % number)
            self.driver.execute_script('arguments[0].click()', trialYear)
            sleep(0.5)
        else:
            self.driver.find_elements_by_css_selector('//*[@[placeholder="%s"]')[number].click()
            sleep(0.5)
            trialYear = self.find_xpath('//*[@x-placement="bottom-start"]/div/div/ul/li[%d]' % number)
            self.driver.execute_script('arguments[0].click()', trialYear)
            sleep(0.5)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    (16)右键点击:

        test = self.driver.find_element_by_xpath(xpath)
        ActionChains(self.driver).context_click(test).perform()
    
    • 1
    • 2

    (17)元素拖动:

        # 定位起始元素
        source = self.driver.find_element_by_xpath(xpath)
        # 让鼠标移动到起点元素上
        pyautogui.moveTo(source.location['x'] + 20, source.location['y'] + 125)
        # 定位要拖拽到的位置元素
        target = self.driver.find_element_by_xpath(target)
        # 实现拖拽功能
        pyautogui.dragTo(target.location['x'] + 20, target.location['y'] + 155, duration=1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    C++ 语法基础课2 —— printf 语句与 C++中的判断结构
    揭秘newSingleThreadExecutor:深度解析与源码探秘
    第 1 章 概述 习题
    【网络协议】Http-中
    配置OSPFv3基本功能 华为笔记
    javaweb 通过JDBC连接Mysql 数据库
    计算机网络——传输层
    06【MyBatis的配置文件】
    深度解析:为什么跨链桥又双叒出事了?
    使用阿里云国际版负载均衡管理多台服务器上的流量
  • 原文地址:https://blog.csdn.net/weixin_43995844/article/details/126448353