元素定位 与Web自动化测试一样,app自动化测试过程中最重要一个环节就是元素定位,只有准确定位到了元素才能进行相关元素的操作,如输入、点击、拖拽、滑动等。appium提供了许多元素定位的方法,如id定位、name定位、class定位、层级定位等等…. 接下来将会给大家来实践运用这些定位技巧。
 
元素定位方式 
id 
name 
class 
List定位 
相对定位 
Xpath定位 
H5页面元素定位 
Uiautomator定位 
 
id定位 日常生活中身边可能存在相同名字的人,但是每个人的身份证号码是唯一的,在app界面元素中也可以使用id值来区分不同的元素,然后进行定位操作。Appium中可以使用   find_element_by_id()  方法来进行id定位。
代码实践 
安装考研帮kaoyan3.1.0.apk 
点击升级页面取消按钮 
点击引导页面的跳 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 from   appium import  webdriverfrom  selenium.common.exceptions import  NoSuchElementExceptiondesired_caps={} desired_caps['platformName' ]='Android'  desired_caps['deviceName' ]='127.0.0.1:62025'  desired_caps['platforVersion' ]='5.1.1'  desired_caps['app' ]=r'C:\Users\Shuqing\Desktop\kaoyan3.1.0.apk'  desired_caps['appPackage' ]='com.tal.kaoyan'  desired_caps['appActivity' ]='com.tal.kaoyan.ui.activity.SplashActivity'  desired_caps['noReset' ]='True'  driver=webdriver.Remote('http://localhost:4723/wd/hub' ,desired_caps) driver.implicitly_wait(5 ) def  check_cancelBtn ():    print ("check_cancelBtn" )     try :         cancelBtn = driver.find_element_by_id('android:id/button2' )     except  NoSuchElementException:         print ('no CancelBtn' )     else :         cancelBtn.click() def  check_skipBtn ():    print ("check_skipBtn" )     try :         skipBtn = driver.find_element_by_id('com.tal.kaoyan:id/tv_skip' )     except  NoSuchElementException:         print ('no skipBtn' )     else :         skipBtn.click() check_updateBtn() check_skipBtn() 
 
name定位 根据name进行定位,对于android来说,就是text属性
用法
1 2 3 4 5 from  find_element.capability import  *driver.find_element_by_name('请输入用户名' ).send_keys('自学网2017' ) driver.find_element_by_name('登录' ).click() 
 
说明:由于text稳定性不是很好,所以appium 1.5开始废弃了该方法。
classname定位 classname定位是根据元素类型来进行定位,但是实际情况中很多元素的classname都是相同的,
如上例中登录页面中的用户名和密码都是clasName属性值都是:android.widget.EditText 因此只能定位第一个元素也就是用户名,而密码输入框就需要使用其他方式来定位,这样其实很鸡肋.一般情况下如果有id就不必使用classname定位。
1 2 3 4 5 from  find_element.capability import  driverdriver.find_element_by_class_name('android.widget.EditText' ).send_keys('自学网2018' ) driver.find_element_by_class_name('android.widget.EditText' ).send_keys('zxw2018' ) driver.find_element_by_class_name('android.widget.Button' ).click() 
 
相对定位 相对定位是先找到该元素的有对应属性的父元素节点,然后基于父元素进行元素定位。
不使用id元素定位方式,在新用户注册界面点击添加头像按钮。
代码实现
1 2 3 4 5 6 from  find_element.capability import  driverdriver.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路径表达式 
表达式 
描述 
 
 
/ 
从根节点选取。 
 
// 
从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。 
 
nodename 
选取此节点的所有子节点。 
 
. 
选取当前节点。 
 
.. 
选取当前节点的父节点。 
 
@ 
选取属性。 
 
xpath匹配符 
|    匹配任何元素节点。 @* |    匹配任何属性节点。 node() |    匹配任何类型的节点。 
 
实践案例 使用xpath定位元素来进行登录操作
1 2 3 4 5 6 7 8 9 from  find_element.capability import  driverdriver.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() 
 
扩展资料:xpath语法  
List定位 前面我们提到相同的classname属性值元素无法区分定位,那么在本节课将使用List定位来解决这个问题。List定位首先是使用find_elements_by_XX获取一组相同的class属性的元素,然后使用数组下标来区分标记不同元素进行相关操作。
测试案例1 在新用户注册界面点击添加头像按钮后,选择指定的图片保存作为头像。
1 2 3 4 5 6 7 8 9 10 11 12 13 from  find_element.capability import  driverdriver.find_element_by_id('com.tal.kaoyan:id/login_register_text' ).click() driver.find_element_by_id('com.tal.kaoyan:id/activity_register_userheader' ).click() images=driver.find_elements_by_id('com.tal.kaoyan:id/item_image' ) images[10 ].click() driver.find_element_by_id('com.tal.kaoyan:id/save' ).click() 
 
测试案例2 测试场景
进入注册界面设置头像 
输入注册信息:用户名、密码、邮箱 
完善院校和专业信息 (院校:上海-同济大学  专业:经济学类-统计学-经济统计学) 
完成注册 
 
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 from  find_element.capability import  driverimport  randomdriver.find_element_by_id('com.tal.kaoyan:id/login_register_text' ).click() driver.find_element_by_id('com.tal.kaoyan:id/activity_register_userheader' ).click() images=driver.find_elements_by_id('com.tal.kaoyan:id/item_image' ) images[10 ].click() driver.find_element_by_id('com.tal.kaoyan:id/save' ).click() username='zxw2018' +'FLY' +str (random.randint(1000 ,9000 )) print ('username: %s'  %username)driver.find_element_by_id('com.tal.kaoyan:id/activity_register_username_edittext' ).send_keys(username) password='zxw' +str (random.randint(1000 ,9000 )) print ('password: %s'  %password)driver.find_element_by_id('com.tal.kaoyan:id/activity_register_password_edittext' ).send_keys(password) email='51zxw' +str (random.randint(1000 ,9000 ))+'@163.com'  print ('email: %s'  %email)driver.find_element_by_id('com.tal.kaoyan:id/activity_register_email_edittext' ).send_keys(email) driver.find_element_by_id('com.tal.kaoyan:id/activity_register_register_btn' ).click() driver.find_element_by_id('com.tal.kaoyan:id/perfectinfomation_edit_school_name' ).click() driver.find_elements_by_id('com.tal.kaoyan:id/more_forum_title' )[1 ].click() driver.find_elements_by_id('com.tal.kaoyan:id/university_search_item_name' )[1 ].click() driver.find_element_by_id('com.tal.kaoyan:id/activity_perfectinfomation_major' ).click() driver.find_elements_by_id('com.tal.kaoyan:id/major_subject_title' )[1 ].click() driver.find_elements_by_id('com.tal.kaoyan:id/major_group_title' )[2 ].click() driver.find_elements_by_id('com.tal.kaoyan:id/major_search_item_name' )[1 ].click() driver.find_element_by_id('com.tal.kaoyan:id/activity_perfectinfomation_goBtn' ).click() 
 
UIAutomator定位简介 UIAutomator元素定位是 Android 系统原生支持的定位方式,虽然与 xpath 类似,但比它更加好用,且支持元素全部属性定位.定位原理是通过android 自带的android uiautomator的类库去查找元素。 Appium元素定位方法其实也是基于Uiautomator来进行封装的。
使用方法 find_element_by_android_uiautomator() 可以运用UiAutomator元素定位。
定位方法包含以下三种:
id定位 id定位是根据元素的resource-id属性来进行定位,使用    UiSelector().resourceId()方法即可。
1 2 3 4 5 6 7 8 9 10 11 from  find_element.capability import  driverdriver.find_element_by_android_uiautomator\     ('new UiSelector().resourceId("com.tal.kaoyan:id/login_email_edittext")' ).send_keys('zxw1234' ) driver.find_element_by_android_uiautomator\     ('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")' ).send_keys('zxw123456' ) driver.find_element_by_android_uiautomator\     ('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")' ).click() 
 
text定位 text定位就是根据元素的text属性值来进行定位,new UiSelector()
1 2 3 driver.find_element_by_android_uiautomator\     ('new UiSelector().text("请输入用户名")' ).send_keys('zxw1234' ) 
 
class name定位 与Appium class定位方式一样,也是根据元素的class属性来进行定位。
1 2 driver.find_element_by_android_uiautomator\     ('new UiSelector().className("android.widget.EditText")' ).send_keys('zxw1234' ) 
 
报错相关 
元素定位报错 
 
1 selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters. 
 
【解决方案】检查元素id值是否写错。
Vivo机型无法点击操作 
 
1 2 Unhandled rejection Error: Error executing adbExec. Original error: 'Command 'E\:\\Andriod_sdk\\platform-tools\\adb.exe -P 5037 -s 7643c658 shell input keyevent 3' exited with code 4294967177'; Stderr: ''; Code: '4294967177'     at ADB.execFunc$ (C:\Users\Shuqing\AppData\Roaming\npm\node_modules\appium\node_modules\_appium-adb@6.9.2@appium-adb\lib\tools\system-calls.js:317:13) 
 
解决方案:打开手机设置中开发者选项-USB模拟点击,如果无法开启,退出vivo账号,重新登录即可。
appium 1.10.1+Uiautomator2兼容问题,具体报错内容如下: 
 
1 Locator Strategy 'css selector' is not supported for this session  
 
解决方案: 在python文件夹下找到site-pankages/selenium/webdriver/remote/webdriver.py中找到def find_element和def find_elements,注释掉 if self.w3c;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54     def find_element(self, by=By.ID, value=None):         """         'Private' method used by the find_element_by_* methods.         :Usage:             Use the corresponding find_element_by_* instead of this.         :rtype: WebElement         """         # if self.w3c:         #     if by == By.ID:         #         by = By.CSS_SELECTOR         #         value = '[id="%s"]' % value         #     elif by == By.TAG_NAME:         #         by = By.CSS_SELECTOR         #     elif by == By.CLASS_NAME:         #         by = By.CSS_SELECTOR         #         value = ".%s" % value         #     elif by == By.NAME:         #         by = By.CSS_SELECTOR         #         value = '[name="%s"]' % value         return self.execute(Command.FIND_ELEMENT, {             'using': by,             'value': value})['value']              def find_elements(self, by=By.ID, value=None):         """         'Private' method used by the find_elements_by_* methods.         :Usage:             Use the corresponding find_elements_by_* instead of this.         :rtype: list of WebElement         """         # if self.w3c:         #     if by == By.ID:         #         by = By.CSS_SELECTOR         #         value = '[id="%s"]' % value         #     elif by == By.TAG_NAME:         #         by = By.CSS_SELECTOR         #     elif by == By.CLASS_NAME:         #         by = By.CSS_SELECTOR         #         value = ".%s" % value         #     elif by == By.NAME:         #         by = By.CSS_SELECTOR         #         value = '[name="%s"]' % value         # Return empty list if driver returns null         # See https://github.com/SeleniumHQ/selenium/issues/4555         return self.execute(Command.FIND_ELEMENTS, {             'using': by,             'value': value})['value'] or []                                           
 
参考资料