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()
相关推荐
刘晓倩5 分钟前
Python3的Sequence
开发语言·python
ZhengEnCi10 分钟前
一次多线程同步问题的排查:从 thread_count 到 thread.join() 的踩坑之旅
python·网络协议·tcp/ip
ULTRA??14 分钟前
ROS Action 完整示例(AI辅助):客户端发目标 + 服务器接参数(lambda 替代 boost::bind)
c++·python
free-elcmacom16 分钟前
用Python玩转GAN:让AI学会“造假”的艺术
人工智能·python·机器学习
计算机毕设匠心工作室26 分钟前
【python大数据毕设实战】全国健康老龄化数据分析系统、Hadoop、计算机毕业设计、包括数据爬取、数据分析、数据可视化、机器学习
后端·python
Dxy12393102161 小时前
Python的PIL对象crop函数详解
开发语言·python
翔云 OCR API1 小时前
护照NFC识读鉴伪接口集成-让身份核验更加智能与高效
开发语言·人工智能·python·计算机视觉·ocr
三好kiii1 小时前
海康威视热成像摄像头温度矩阵提取实战:ISAPI + Python 实现无 SDK 读取
图像处理·python
logocode_li1 小时前
面试 LoRA 被问懵?B 矩阵初始化为 0 的原因,大多数人拿目标来回答
人工智能·python·面试·职场和发展·矩阵