Appium学习笔记11-定位一个或一组界面元素(控件)

1利用UiAutomatorViewer获取元素(控件)的id、class等信息

在这里插入图片描述

2.利用id、class、xpath方法定位一个元素

driver.find_element_by_id(id_value)#根据id定位元素
driver.find_element_by_class_name(class_name_value)#根据class定位元素
driver.find_element_by_xpath(id_value)#根据xpath定位元素

打开手机设置,对弹出的授权框点击确定;
点击搜索放大镜;
在这里插入图片描述
输入框中输入hello。
在这里插入图片描述
代码如下:

#导入库
from appium import webdriver
import time

desired_caps = dict()#创建字典
desired_caps['platformName'] = 'Android'#添加字典字段:手机平台(Android、iOS)
desired_caps['platformVersion'] = '5.1'#添加字典字段:系统版本号(可从手机的设置里面查看)
desired_caps['deviceName'] = 'myphone'#添加字典字段:设备名称(随便写即可)
desired_caps['appPackage'] = 'com.android.settings'#添加字典字段:要打开的app包名
desired_caps['appActivity'] = 'com.android.settings.Settings'#添加字典字段:APP的界面名
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)#调用方法,其中'http://localhost:4723/wd/hub'即为appium服务器地址

driver.find_element_by_id("android:id/button1").click()#根据id定位元素,弹出应用的授权框,点击“确认”
driver.find_element_by_xpath("//*[@content-desc='搜索']").click()#根据xpath定位搜索的放大镜图标,并点击
driver.find_element_by_class_name("com.meizu.common.widget.SearchEditText").send_keys("hello")#根据class定位元素,并键入“hello”

driver.quit()#退出此次驱动连接

3.利用id、class、xpath定位一组元素

driver.find_elements_by_id(id_value)#得到相同id的元素集合列表
driver.find_elements_by_class_name(class_name_value)#得到相同class的元素集合列表
driver.find_elements_by_xpath(id_value)#得到相同xpath的元素集合列表

在这里插入图片描述
代码:

#导入库
from appium import webdriver
import time

desired_caps = dict()#创建字典
desired_caps['platformName'] = 'Android'#添加字典字段:手机平台(Android、iOS)
desired_caps['platformVersion'] = '5.1'#添加字典字段:系统版本号(可从手机的设置里面查看)
desired_caps['deviceName'] = 'myphone'#添加字典字段:设备名称(随便写即可)
desired_caps['appPackage'] = 'com.android.settings'#添加字典字段:要打开的app包名
desired_caps['appActivity'] = 'com.android.settings.Settings'#添加字典字段:APP的界面名
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)#调用方法,其中'http://localhost:4723/wd/hub'即为appium服务器地址

driver.find_element_by_id("android:id/button1").click()#根据id定位元素,弹出应用的授权框,点击“确认”

#获取所有id为title的元素,并打印出出来
print("----以下为id为title----")
titles=driver.find_elements_by_id("com.android.settings:id/title")#根据id定位元素,弹出应用的授权框,点击“确认”
for title in titles:#循环遍历
    print(title.text, end="、")#输出

#获取所有class为TextVie的元素,并打印出出来
print()#回车
print("----以下为class为TextVie----")
TextVies=driver.find_elements_by_class_name("android.widget.TextView")#根据class定位元素,并键入“hello”
for TextVie in TextVies:#循环遍历
    print(TextVie.text, end="、")#输出

#获取所有test中包含“设”字的元素,并打印出出来
print()#回车
print("----以下为包含“设”字的元素----")
shes=driver.find_elements_by_xpath("//*[contains(@text,'设')]")
for she in shes:#循环遍历
    print(she.text, end="、")#输出

driver.quit()#退出此次驱动连接

4.定位元素注意点

4.1定位元素一定要是当前界面可见元素

一定要是当前页面可见才行,下滚可见或者下拉可见都不行,从定位一组元素的结果可以看出。

4.2定位一个元素时,定位条件一定在当前界面只有唯一匹配值

若定位条件在当前界面存在多个匹配值,获取到的是第一个匹配到的元素

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值