App自动化的元素定位

一.Appium定位步骤

打开appium,输入本地IP,点击启动服务器

1.点击启动检查器会话

2.配置所需功能,点击启动会话

二.App页面元素

App页面元素分为布局和控件两种

1.布局

FrameLayout(框架布局)

LinearLayout(线性布局)

AbsoluteLayout(绝对布局)

RelativeLayout(相对布局)

TableLayout(表格布局)

2.控件

ButtonImageView、EditText、TextView...

三.元素定位方法

1.id定位

通过resource-id定位,但是一个页面中可能存在不止一个相同的resource-id

By by=MobileBy.id("com.cntaiping.tpl.tptb:id/tv_switch_account");

2.Text定位

MobileBy.AndroidUIAutomator("new UiSelector().text(\"密码登录\")"

3.Classname定位

通过class属性定位,但是class属性可能不唯一

MobileBy.className("android.widget.LinearLayout")

4.Accessibility id定位:content desc

MobileBy.AccessibilityId(\"题库\")

5.xpath定位

1xpath绝对路径

从根目录开始找:html/body...

(2)Xpath相对路径

优点:灵活,轻便,耦合性低

//标签名[@属性名=”属性值”]

//匹配指定节点,不考虑他们的位置;

标签名:可以是具体的标签名,也可以是*通配符,表示任意标签名;

@选取属性

1)元素标签名定位

MobileBy.xpath("//iframe")

2)元素名+索引定位

MobileBy.xpath("//*/android.widget.LinearLayout/android.widget.TextView[1]")

3)属性

MobileBy.xpath("//*[@class='gloable-upload']/i")

4)元素标签+属性

MobileBy.xpath("//input[@placeholder='请输入标题']")

5)包含属性部分值

MobileBy.xpath("//div[contains(@class,'btn-submit')]")

6)元素文本内容

MobileBy.xpath("//span[text()='新建']")

7)包含元素文本部分值

MobileBy.xpath("//div[contains(text(),'登 录')]")

3Xpath轴定位

轴名称

释义

ancestor

选取当前节点的所有祖先节点(包括父节点) --找所有的祖先

parent

选取当前节点的父节点 -- 找爸爸

preceding

选取当前节点之前的所有节点

preceding-sibling

选取当前节点之前的所有兄弟节点 --找哥哥

following

选取当前节点之后的所有节点

following-sibling

选取当前节点之后的所有兄弟节点 --找弟弟 

/轴名称::节点名称[@属性=值]

By.xpath("//*[text()='资讯平台']//preceding-sibling::div/img")

 

posted @ 2021-06-08 15:17  思佳丽  阅读(192)  评论(0)    收藏  举报