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()
相关推荐
锋行天下12 小时前
服务器 Python 2.7 装不了 langgraph-cli?用本地生成 Dockerfile 曲线救国
人工智能·后端·python
一条大祥脚12 小时前
深入理解 PyTorch FlexAttention:从 Score Mod 到 Mask Mod 的稀疏加速之道
人工智能·pytorch·python·注意力机制·flex attention·可编程注意力
2601_9679801113 小时前
Python自动化办公:模块、库与样式调整教程
python·excel·自动化办公·openpyxl·样式调整
长沙三为智能科技13 小时前
GEO与传统SEO的5个关键差异:排名与被引用、关键词与答案型的全维度对比
python·aigc·seo·ai搜索·geo优化
量化分析码农15 小时前
【Python量化趋势跟踪实战 02】突破买入总被假突破坑:通道系统与入场时机
python
Agent世界15 小时前
多平台登录态巡检:cookie 判据为什么会撒谎
python
量化分析码农16 小时前
【Python量化趋势跟踪实战 01】均线多头排列就是趋势开始吗:过滤与确认
python
用户2986985301416 小时前
Python 实战:把代码写入 Word 文档的两种实现方式
后端·python·api
小小张说故事16 小时前
Python 里 pickle 到底存了什么:拆开 __reduce__、__getstate__ 和那串能执行代码的字节码
python
reasonsummer16 小时前
【教学类-150-03】20261108蝴蝶连连看html(一页2个版面、3个版面)
python·html