0


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=‘’]")

class属性

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

name属性

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

常用操作

(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()

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

(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)

在这里插入图片描述

(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()

在这里插入图片描述
在这里插入图片描述
(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.下拉选择
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

(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()

(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)

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

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

(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()))

(10)获取明天日期:

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

(11)获取日期:

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

(12)上传文件:

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

(13)验证列表数据:

    text_data = self.driver.find_element_by_xpath(xpath).text
    print(text_data)
    assert text == text_data

(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()

(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

(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()

(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)

(16)右键点击:

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

(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)

本文转载自: https://blog.csdn.net/weixin_43995844/article/details/126448353
版权归原作者 ability 所有, 如有侵权,请联系我们删除。

“UI自动化”的评论:

还没有评论