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()
相关推荐
Hylan_J1 小时前
【VSCode】MicroPython环境配置
ide·vscode·python·编辑器
莫忘初心丶1 小时前
在 Ubuntu 22 上使用 Gunicorn 启动 Flask 应用程序
python·ubuntu·flask·gunicorn
失败尽常态5234 小时前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_904447744 小时前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农4 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿4 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing5 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
xinxiyinhe6 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
诸神缄默不语6 小时前
如何用Python 3自动打开exe程序
python·os·subprocess·python 3
橘子师兄7 小时前
分页功能组件开发
数据库·python·django