自动化测试之iOS元素定位

本文ios元素定位用的是Appium

appium中的元素为WebElement

WebElement对象代表了一个DOM元素。

一、WebElement的属性

tag_name --- 元素的tagName属性
text --- 元素的文本内容
location_once_scrolled_into_view --- 滚动直到指定的元素在视图中
size --- 元素的大小
location --- 元素在画布中的位置
rect --- 元素的大小和位置
screenshot_as_base64 --- 元素显示图像的二进制数据的base64编码字符串
screenshot_as_png --- 元素显示图像的PNG格式二进制数据
parent --- 元素的父元素对象
id --- 元素在Selenium中的内部ID,并非在DOM中的id属性。

二、WebElement的属性和状态操作

get_property --- 获得元素指定名称的属性。(property是DOM中的属性,像是JavaScript里的对象,只要是某类型的对象就自动具备这些属性成员。)
get_attribute --- 获得元素指定名称的特性。(attribute是HTML标签上的特性,它的值只能够是字符串,由用户额外设定的特性名称和特性值。)
is_selected --- 获得元素的选中状态,特指Select类型的元素,比如checkbox和radio。
is_enabled --- 获得元素的使能状态
is_displayed --- 元素是否对用户可见
value_of_css_property --- 元素的CSS属性中指定名称的属性值

三、WebElement的行为

click --- 点击元素
submit --- 提交表格
clear --- 清除文本输入
send_keys --- 模拟键盘向元素输入内容
screenshot --- 元素的显示图像保持为PNG格式文件

四、如何定位元素

1.最常用的

self.driver.find_element_by_name("登 录")
self.driver.find_element_by_class_name("XCUIElementTypeCell").click()

find_elment_by_xxx,返回满足条件的第一个元素

2.若是遇到无法通过某些条件定位的元素,只能通过坐标定位

for ele in self.driver.find_elements_by_class_name("XCUIElementTypeImage"):
    if ele.location.get('x') == 375 and ele.location.get('y') == 293:
        ele.click()
        break

3.判断元素是否存在

    def elementExist(self, identifyBy, el):
        flag = None
        try:
            if identifyBy == "id":
                # self.driver.implicitly_wait(60)
                self.driver.find_element_by_id(el)
            elif identifyBy == "xpath":
                # self.driver.implicitly_wait(60)
                self.driver.find_element_by_xpath(el)
            elif identifyBy == "class":
                self.driver.find_element_by_class_name(el)
            elif identifyBy == "link text":
                self.driver.find_element_by_link_text(el)
            elif identifyBy == "partial link text":
                self.driver.find_element_by_partial_link_text(el)
            elif identifyBy == "name":
                self.driver.find_element_by_name(el)
            elif identifyBy == "tag name":
                self.driver.find_element_by_tag_name(el)
            elif identifyBy == "css selector":
                self.driver.find_element_by_css_selector(el)
            flag = True
        except NoSuchElementException as e:
            flag = False
        finally:
            return flag

你可能感兴趣的:(自动化测试)