Appium自动化测试(十二)Appium元素定位

ID元素定位及元素检测

使用异常处理来检测是否定位到了相应的元素。
id值可以通过Appium或uiautomatorviewer进行查找

from appium import webdriver
from time import sleep
from selenium.common.exceptions import NoSuchElementException

desired_caps = {}
desired_caps['platformName']= "Android"
链接模拟机(逍遥模拟器,端口号为21503,21513,21523以此类推)
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = '127.0.0.1:21503'

# desired_caps['platformVersion'] = '6.0'
# desired_caps['deviceName'] = '红米手机'
# desired_caps['udid'] = 'AAYDAUV87S5H8T95'

# desired_caps['app'] = r"C:\Users\Administrator\Desktop\com.hpbr.bosszhipin-7.160-360.apk"
desired_caps['appPackage'] = 'com.hpbr.bosszhipin'
desired_caps['appActivity'] = 'com.hpbr.bosszhipin.module.launcher.WelcomeActivity'

# desired_caps['noReset'] = 'True'   # True时不重置会话,False重置会话。默认值为False,
# 此处设置为True,打开界面直接进入登录界面,不再选择角色。

webdr = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
webdr.implicitly_wait(30)
# 选择角色(应聘者),使用异常处理来检测是否定位到了相应的元素
try:
    choose_actor = webdr.find_element_by_id("com.hpbr.bosszhipin:id/btn_enter_geek")
except NoSuchElementException:
    print('no actorbtn')
else:
    choose_actor.click()
sleep(5)
# 选择登录方式
webdr.find_element_by_id('com.hpbr.bosszhipin:id/tv_password_login').click()
sleep(5)
webdr.quit()

name定位

根据name进行定位,对于android来说,就是text属性

driver.find_element_by_name('请输入您的手机号码').send_keys('15610273654')
 driver.find_element_by_name('登录').click()

说明:由于text稳定性不是很好,所以appium 1.5开始废弃了该方法。

classname

classname定位是根据元素类型来进行定位,但是实际情况中很多元素的classname都是相同的,如上例中登录页面中的用户名和密码都是clasName属性值都是:“android.widget.EditText” 因此只能定位第一个元素也就是用户名,而密码输入框就需要使用其他方式来定位,这样其实很鸡肋.一般情况下如果有id就不必使用classname定位。

from find_element.capability import driver driver.find_element_by_class_name('android.widget.EditText').send_keys('15610273654')
driver.find_element_by_class_name('android.widget.EditText').send_keys('888888')  # 无法定位到密码框,实际执行时,将密码输入到了账号框中
driver.find_element_by_class_name('android.widget.Button').click()

相对定位

相对定位是先找到该元素的有对应属性的父元素节点,然后基于父元素进行元素定位。

from find_element.capability import driver 
#点击注册按钮
driver.find_element_by_id('com.tal.kaoyan:id/login_register_text').click()      
# 首先获取添加头像的父元素
root_element=driver.find_element_by_id('com.tal.kaoyan:id/activity_register_parentlayout')   
#通过父元素获取添加头像的元素
root_element.find_element_by_class_name('android.widget.ImageView').click() 

xpath定位

xpath定位是一种路径定位方式,主要是依赖于元素绝对路径或者相关属性来定位,但是绝对路径xpath执行效率比较低(特别是元素路径比较深的时候),一般使用比较少。通常使用xpath相对路径和属性定位。

xpath详细信息参考W3School内容

driver.find_element_by_xpath('//android.widget.EditText[@text="请输入用户名"]').send_keys('zxw1234') 
driver.find_element_by_xpath('//*[@class="android.widget.EditText" and @index="3"]').send_keys('zxw123456') driver.find_element_by_xpath('//android.widget.Button').click() 
# driver.find_element_by_xpath('//*[@class="android.widget.Button"]').click()

List定位

List定位首先是使用find_elements_by_XX获取一组相同的class属性的元素,然后使用数组下标来区分标记不同元素进行相关操作。

UIAutomator定位

使用方法 find_element_by_android_uiautomator()

id

id定位是根据元素的resource-id属性来进行定位,使用 UiSelector().resourceId()方法

from appium import webdriver
from time import sleep
desired_caps = {}
desired_caps['platformName']= "Android"
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = '127.0.0.1:21503'
desired_caps['appPackage'] = 'com.hpbr.bosszhipin'
desired_caps['appActivity'] = 'com.hpbr.bosszhipin.module.launcher.WelcomeActivity'
# desired_caps['noReset'] = 'True'   # True时不重置会话,False重置会话。默认值为False,
# 此处设置为True,打开界面直接进入登录界面,不再选择角色。

webdr = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
webdr.implicitly_wait(30)

webdr.find_element_by_id("com.hpbr.bosszhipin:id/btn_enter_geek").click()

sleep(5)
webdr.find_element_by_android_uiautomator('new UiSelector().resourceId("com.hpbr.bosszhipin:id/et_phone")').send_keys('15610273654')   
# 注意字母大小写,同时建议webdr.find_element_by_android_uiautomator()中参数用双引号,resourceId()参数用单引号。否则有时候会报错。
sleep(5)
webdr.quit()

text

text定位text定位就是根据元素的text属性值来进行定位,new UiSelector().text()

webdr.find_element_by_android_uiautomator('new UiSelector().text("请输入您的手机号码")').send_keys('15610273654')

Class name

与Appium class定位方式一样,也是根据元素的class属性来进行定位。new UiSelector().className()

webdr.find_element_by_android_uiautomator('new UiSelector().className("android.widget.EditText")').send_keys('15610273654')

元素等待

强制等待

设置固定的等待时间,使用sleep()方法即可实现
from time import sleep
#强制等待5秒
sleep(5)

隐式等待

隐式等待是针对全部元素设置的等待时间
driver.implicitly_wait(20)

显式等待

显式等待是针对某个元素来设置的等待时间。

from selenium.webdriver.support.ui import WebDriverWait 
WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
driver : WebDriver
timeout : 最长超时时间,默认以秒为单位
poll_frequency : 休眠时间的间隔时间,默认为0.5秒
ignored_exceptions : 超时后的异常信息,默认情况下抛NoSuchElementException异常。

WebDriverWait()一般和until()或until_not()方法配合使用,另外,lambda提供了一个运行时动态创建函数的方法。

from selenium.webdriver.support.ui import WebDriver
WaitWebDriverWait(driver,10).until(lambda x:x.find_element_by_id("elementID"))

toast识别

Appium 1.6.3开始支持识别Toast内容,主要是基于UiAutomator2,因此需要在Capablity配置如下参数:
desired_caps[‘automationName’]=‘uiautomator2’

1、装appium-uiautomator2-driver: 安装命令如下:
cnpm install appium-uiautomator2-driver

安装成功后可以在 C:\Users\XXXX\node_modules看到对应的文件:
_appium-uiautomator2-driver@1.12.0@appium-uiautomator2-driver
 _appium-uiautomator2-server@1.10.0@appium-uiautomator2-server

2、安装selenium模块pip install selenium

安装完成后使用如下命令检测是否安装成功
pip show selenium

案例:

from appium import webdriver
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait   # 显示等待

desired_caps = {}
desired_caps['platformName']= "Android"

desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = '127.0.0.1:21503'
desired_caps['automationName']='uiautomator2'


desired_caps['appPackage'] = 'com.hpbr.bosszhipin'
desired_caps['appActivity'] = 'com.hpbr.bosszhipin.module.launcher.WelcomeActivity'

# desired_caps['noReset'] = 'True'   # True时不重置会话,False重置会话。默认值为False,
# 此处设置为True,打开界面直接进入登录界面,不再选择角色。

webdr = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
webdr.implicitly_wait(30)        # 隐式等待,所有元素定位最大等待时间为30s

# 角色选择(应聘者)
webdr.find_element_by_id("com.hpbr.bosszhipin:id/btn_enter_geek").click()
sleep(5)
# 登录方式选择(账号密码登录)
webdr.find_element_by_id("com.hpbr.bosszhipin:id/tv_password_login").click()
sleep(2)

# 定位账号输入框,并输入账号
webdr.find_element_by_id("com.hpbr.bosszhipin:id/et_phone").send_keys("13945280649")
sleep(1)
# 定位密码框,并输入密码
webdr.find_element_by_id("com.hpbr.bosszhipin:id/et_password").send_keys("123456987")
sleep(1)
# 点击登录
webdr.find_element_by_id("com.hpbr.bosszhipin:id/tv_login").click()

# toast 定位
note_message = "账号不存在"    #首先定义toast显示内容文本变量

note_xpath = "//*[@text=\'{}\']".format(note_message) # 定义xpath路径——根据文本属性

toast_element = WebDriverWait(webdr,5).until(lambda x:x.find_element_by_xpath(note_xpath))

print(toast_element.text)    # 打印获取元素的文本信息

sleep(5)
webdr.quit()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值