appium元素定位 - 余生没有余生
source link: https://www.cnblogs.com/lihongtaoya/p/16988850.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
appium元素定位
本章主要介绍几种原生app定位的方法,我们可以进入npm安装目录下找到driver.js文件打开可以
看到如下几种定位方法,我的地址是(C:\Users\username\AppData\Roaming\npm\node_modules
\appium\node_modules\appium-android-driver\lib),npm版本为8.1.2。
import time from appium import webdriver from appium.webdriver.common.appiumby import AppiumBy desired_cap = { "platformName": "Android", # 手机系统 "platformVersion": "11", # 手机系统版本 "deviceName": 'xxxxx', # 连接的设备 "automationName": "UiAutomator2", # 自动化测试框架 "appPackage": "xxxxx", # app包名 "appActivity": "xxxxxx", # app的启动页面 "autoGrantPermissions": "true", # 默认允许app获取相关权限 "noReset": True # 保留登录模式 } driver = webdriver.Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_cap) driver.implicitly_wait(10) # 设置元素等待 driver.find_element(AppiumBy.ID, "tv.danmaku.bili:id/agree").click() # id定位resource-id
id的值与uiautomatorviewer中resource-id的值一致。resource-id在元素app中并不是唯一的,所以使用要视情况而定。
2.xpath
xpath可根据uiautomatorviewer中node detail的属性和值进行定位
1)单条件定位
driver.find_element(AppiumBy.XPATH, '//*[@text="同意并继续"]').click()
2)多条件定位
driver.find_element(AppiumBy.XPATH, '//*[@text="同意并继续" and @resource-id="tv.danmaku.bili:id/agree"]').click()
多个条件之间用and连接
3.ANDROID_UIAUTOMATOR
1)根据文本(text)值进行定位
import time from appium import webdriver from appium.webdriver.common.appiumby import AppiumBy desired_cap = { "platformName": "Android", # 手机系统 "platformVersion": "11", # 手机系统版本 "deviceName": 'xxxxx', # 连接的设备 "automationName": "UiAutomator2", # 自动化测试框架 "appPackage": "xxxxx", # app包名 "appActivity": "xxxxxx", # app的启动页面 "autoGrantPermissions": "true", # 默认允许app获取相关权限 "noReset": True # 保留登录模式 } driver = webdriver.Remote(command_executor="http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_cap) driver.implicitly_wait(10) # 设置元素等待 driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("同意并继续")').click() # text值精准匹配
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textContains("意并继")').click() # text值模糊匹配
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().textStartsWith("同意")').click() # text值匹配开头
2)根据class_name进行定位
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().className("android.widget.TextView")').click()
class_name对应的就是class的值
3)根据resource-id进行定位
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("tv.danmaku.bili:id/agree")').click()
4)根据description进行定位
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().description("Xxxx")').click()
description对应的值就是content-desc
5)多条件定位
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().className("android.widget.TextView").text("动画").resourceId("tv.danmaku.bili:id/tab_title")').click() # 组合使用
多个条件判断中用.号连接
4.ACCESSIBILITY_ID
accessibility_id对应的值为content_desc
driver.find_element(AppiumBy.ACCESSIBILITY_ID, "动画,7之4,标签").click()
5.class_name
这个定位强烈不推荐使用,贼垃圾,多半有重复的值,定位多数不准确
driver.find_elements(AppiumBy.CLASS_NAME, "android.widget.TextView")
class_name对应的值为class
6.h5方面的定位可参考selenium定位方法
https://www.cnblogs.com/lihongtaoya/p/16487846.html
文章来源:https://www.cnblogs.com/lihongtaoya/ ,请勿转载
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK