Appium常用的使用方法(一)

一: Appium 定位元素

定位元素的步骤

1、启动 Appium Inspector:

启动 Appium Desktop 应用程序并连接到你的设备或模拟器。

输入你的 Desired Capabilities,并点击 "Start Session"。

2、查看应用的 UI 层级结构:

应用启动后,Appium Inspector 会显示一个树状结构,表示应用中所有 UI 元素的层次关系。

你可以在这个视图中查看所有可交互的元素,比如按钮、文本框、列表项等。

3、选择并查看元素:

鼠标移到树状结构中的任何元素上时,会高亮显示对应的 UI 元素。

点击某个元素后,右侧面板会显示该元素的详细属性,比如 ID、XPath、Class Name、Text、Resource ID 等。

4、使用元素属性进行定位:

根据查看到的属性,你可以使用以下几种方式在自动化测试代码中定位元素:

常用的定位方式

1、通过 ID 定位:

复制代码
element = driver.find_element(By.ID, "你的元素ID")

2、通过 Name 或 Accessibility ID 定位:

python 复制代码
element = driver.find_element(By.ACCESSIBILITY_ID, "你的元素Name")

3、通过 XPath 定位:

python 复制代码
element = driver.find_element(By.XPATH, "//android.widget.Button[@text='点击我']")

4、通过 Class Name 定位:

python 复制代码
element = driver.find_element(By.CSS_SELECTOR, "button[class='btn-class']")

5、通过 CSS 选择器(仅适用于某些平台):

python 复制代码
element = driver.find_element(By.CSS_SELECTOR, "button[class='btn-class']")

示例

python 复制代码
from appium import webdriver
from selenium.webdriver.common.by import By
import time

# 设置 Desired Capabilities
desired_caps = {
    "platformName": "Android",
    "platformVersion": "11.0",
    "deviceName": "Pixel_3a",
    "app": "/path/to/your.app",
    "automationName": "UiAutomator2"
}
# 初始化 Appium Driver
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
try:
    # 等待应用加载
    time.sleep(5)
    # 通过 ID 定位元素并点击
    element = driver.find_element(By.ID, "com.example.yourapp:id/button1")
    element.click()
    # 通过 XPath 定位元素并输入文本
    input_element = driver.find_element(By.XPATH, "//android.widget.EditText")
    input_element.send_keys("Hello World")
finally:
    # 关闭驱动
    driver.quit()

二:Appium执行过程中等待元素加载出来的常用方式

方式一:time.sleep() 设置固定的等待时间

复制代码
import time
time.sleep(10) # 等待10S

方式二:显示等待

python 复制代码
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, 10)
input_element = wait.until(EC.visibility_of_element_located((By.ID, "your_input_field_id")))
wait.until(EC.element_to_be_clickable((By.ID, "your_input_field_id")))

方式三:隐式等待

在 Appium 中,隐式等待是一个用于设置 WebDriver 实例在查找元素时等待的时间。隐式等待会告诉 WebDriver 在查找元素时,如果未立即找到,请在给定的时间内反复查找,直到找到为止。隐式等待对全局适用,也就是说,一旦设置后,所有调用 find_element 和 find_elements 方法都会遵循这一等待时间。

python 复制代码
from appium import webdriver
import time

# 设置 Desired Capabilities
desired_caps = {
    "platformName": "Android",  # 或 "iOS"
    "deviceName": "你的设备名称",  # 替换为你的设备名称
    "app": "你的应用路径或包名",  # 替换为你的应用路径或包名
    "automationName": "UiAutomator2"  # Android 使用 UiAutomator2,iOS 使用 XCUITest
}
# 初始化 Appium Driver
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
# 设置隐式等待时间
driver.implicitly_wait(10)  # 等待 10 秒(这意味着 WebDriver 会在查找每个元素时最多等待 10 秒。)
try:
    # 打开某个页面
    time.sleep(5)  # 完全加载页面等待
    # 查找元素,隐式等待会自动生效
    element = driver.find_element_by_id("your_element_id")  # 替换为你的元素 ID
    element.click()  # 执行某个操作
finally:
    # 关闭驱动
    driver.quit()

三:Appium滑动页面

TouchAction 是 Appium 提供的一个工具类,可以模拟触摸操作,例如滑动。以下是使用 TouchAction 滑动页面到底部的示例代码:

python 复制代码
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
import time
# 设置 Desired Capabilities
desired_caps = {
    "platformName": "Android",  
    "deviceName": "你的设备名称",  
    "app": "你的应用路径或包名",
    "automationName": "UiAutomator2"
}
# 初始化 Appium Driver
driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps)
try:
    # 等待一段时间以保证应用加载完成
    time.sleep(5)
    # 获取屏幕的宽和高,以用于滑动
    size = driver.get_window_size()
    width = size['width']
    height = size['height']
    # 计算滑动的起始和结束点
    start_x = width / 2  # 从屏幕中间的 X 轴
    start_y = height * 0.8  # 从屏幕下方 80% 的位置开始滑动
    end_y = height * 0.2  # 滑动到屏幕上方 20% 的位置
    # 创建 TouchAction 实例并执行滑动
    actions = TouchAction(driver)
    actions.press(x=start_x, y=start_y).wait(1000).move_to(x=start_x, y=end_y).release().perform()
    # 你可以选择等待一段时间以查看滑动效果
    time.sleep(2)
finally:
    # 关闭驱动
    driver.quit()
相关推荐
databook5 小时前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar6 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户8356290780516 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_6 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机13 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机14 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机14 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机14 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i14 小时前
drf初步梳理
python·django
每日AI新事件14 小时前
python的异步函数
python