Python Appium 元素定位
常用的八种定位方法(与selenium通用)
# id定位 driver.find_element_by_id() # name定位 driver.find_element_by_name() # xpath定位 driver.find_element_by_xpath() # class_name定位 driver.find_element_by_class_name() # css 定位 driver.find_element_by_css_selector() # link定位 driver.find_element_by_link_text() # tag定位 driver.find_element_by_tag_name() # partial_link定位 driver.find_element_by_partial_link_text()
# 移动端 新增方法 # accessibility_id 定位 driver.find_element_by_accessibility_id() # Android driver.find_element_by_android_uiautomator() # ios driver.find_element_by_ios_uiautomation()
id定位
# resrouce-id属性是id: driver.find_element_by_id(‘username‘)
name定位
# text属性是name driver.find_element_by_name(u‘请输入用户名‘)
Xpath定位
如下图,使用 uiautomatorviewer.bat 获取页面元素
注意:SDK自带的uiautomatorviewer.bat无法获取xpath,需要替换一个jar包方可获取xpath。此jar包放到了QQ群:193056556中,有需要的可以进群自取。
self.driver.find_element_by_xpath("//android.widget.TextView[@text=‘ ‘]").click()
class_name定位
self.driver.find_element_by_class_name(‘android.view.View‘).click()
(注:一般一个页面上的class属性不唯一,元素不唯一的话定位会报错了)
accessibility_id 定位
content-desc即是对应的 accessibility_id
driver.find_element_by_accessibility_id(u"我的").click()
android_uiautomator 定位
driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear()
以下是代码实例
#测试异常登录-用户不存在 def test_aNoUserLogin(self): driver = self.driver time.sleep(5) driver.find_element_by_accessibility_id(u"我的").click() time.sleep(5) driver.find_element_by_accessibility_id(u"登录/注册").click() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).send_keys(‘xzbuyer1‘) time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).send_keys(‘111111‘) time.sleep(2) driver.find_element_by_accessibility_id(u"登录").click() time.sleep(18) get_alertMessage = driver.find_element_by_id(‘android:id/message‘).text time.sleep(2) if get_alertMessage == u‘用户不存在‘: print (u"用户不存在,用例执行成功!") else: print (u"用例执行失败!") def test_bNoPassLogin(self): driver = self.driver time.sleep(5) driver.find_element_by_accessibility_id(u"我的").click() time.sleep(5) driver.find_element_by_accessibility_id(u"登录/注册").click() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).send_keys(‘xzbuyer‘) time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).send_keys(‘1111112‘) time.sleep(2) driver.find_element_by_accessibility_id(u"登录").click() time.sleep(18) get_alertMessage = driver.find_element_by_id(‘android:id/message‘).text time.sleep(2) print (get_alertMessage[0:5]) # if get_alertMessage == u‘用户不存在‘: # print u"用户不存在,用例执行成功!" # else: # print u"用例执行失败!" # # 测试正常登录 def test_cLogin(self): driver = self.driver time.sleep(5) driver.find_element_by_accessibility_id(u"我的").click() time.sleep(5) driver.find_element_by_accessibility_id(u"登录/注册").click() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("username")‘).send_keys(‘xzbuyer‘) time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).clear() time.sleep(2) driver.find_element_by_android_uiautomator(‘new UiSelector().resourceId("password")‘).send_keys(‘111111‘) time.sleep(2) driver.find_element_by_accessibility_id(u"登录").click() time.sleep(2) # 判断是否登录成功 print (driver.find_element_by_accessibility_id(u"西藏医药销售公司(测试买家)").get_attribute(‘name‘)) nickName = driver.find_element_by_accessibility_id(u"西藏医药销售公司(测试买家)").get_attribute(‘name‘) temp = nickName[0:2] print (temp) if nickName == u‘西藏医药销售公司(测试买家)‘: print (‘登录成功!‘) else: print (‘登录失败!‘) def tearDown(self): self.driver.close_app() self.driver.quit()