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()
相关推荐
Salt_07281 小时前
DAY 19 数组的常见操作和形状
人工智能·python·机器学习
无心水1 小时前
【Python实战进阶】2、Jupyter Notebook终极指南:为什么说不会Jupyter就等于不会Python?
python·jupyter·信息可视化·binder·google colab·python实战进阶·python工程化实战进阶
上班日常摸鱼2 小时前
Shell脚本基础教程:变量、条件判断、循环、函数实战(附案例)
python
无心水3 小时前
【Python实战进阶】5、Python字符串终极指南:从基础到高性能处理的完整秘籍
开发语言·网络·python·字符串·unicode·python实战进阶·python工业化实战进阶
2301_807583233 小时前
了解python,并编写第一个程序,常见的bug
linux·python
小白学大数据3 小时前
构建混合爬虫:何时使用Requests,何时切换至Selenium处理请求头?
爬虫·python·selenium·测试工具
2401_827560203 小时前
【Python脚本系列】PyAudio+librosa+dtw库录制、识别音频并实现点击(四)
python·语音识别
BBB努力学习程序设计3 小时前
Python自动化脚本:告别重复劳动
python·pycharm
BBB努力学习程序设计3 小时前
Python函数式编程:优雅的代码艺术
python·pycharm
2501_940943913 小时前
体系课\ Python Web全栈工程师
开发语言·前端·python