appium===常用方法介绍,元素定位
https://testerhome.com/topics/3711
元素定位方法:
find_element_by_android_uiautomator ,使用uiautomator定位,后面参数更改即可:
-
UiSelector().text 根据text属性进行定位
self.driver.find_element_by_android_uiautomator('new UiSelector().text("33001122")')
-
UISelector.textContains 根据text属性模糊定位
e6 = self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("1122")')
-
UISelector.textStartsWith 根据text的前面几位是否与text一致来定位
e7 = self.driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("33")')
-
UISelector.textMatches 通过正则表达式和text来进行定位,正则不怎么会,先不扩展了
-
UISelector.className 通过class来进行定位,合理利用层级定位,例如找到所有的Edittext然后根据text定位
e8 = self.driver.find_element_by_android_uiautomator('new UiSelector()' '.className("android.widget.EditText")' '.textContains("33")')
-
UISelector.classNameMatches 通过正则表达式和class来进行定位
还有UiSelector.descriptionMatches 、 UiSelector.descriptionStartWith 、UiSelector.description
from appium import webdriver from time import sleep s_time = 3 desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '7' desired_caps['deviceName'] = 'ssssssssssssssssss' desired_caps['appPackage'] = 'com.android.calculator2' desired_caps['appActivity'] = '.Calculator' driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # by_id driver.find_element_by_id("digit_1").click() # by_xpath driver.find_element_by_xpath("//android.widget.Button[@text='5']").click() driver.find_element_by_id("digit_9").click() # by_accessibility_id driver.find_element_by_accessibility_id("删除").click() # by_class_name driver.find_elements_by_class_name("android.widget.Button")[10].click() print("pass") driver.find_element_by_id("digit_5").click() # by_android_uiautomator driver.find_element_by_android_uiautomator('new UiSelector().text("9")').click() driver.find_element_by_id("digit_6").click() driver.find_element_by_id("eq").click() sleep(s_time) driver.quit()
纸上得来终觉浅,绝知此事要躬行!