问题遇到场景:
打开 PyCharm,创建一个 python 项目,用 Inspector 录制生成的 python 脚本代码复制到该 python 中,导入 appium 相关内容,如appium 的 webdriver等无法联想使用,发现自己未安装 appium 插件,无法使用运行脚本测试。
安装方法:
打开 python 项目下方的终端,命令行安装:pip install Appium-Python-Client
(.venv) nhl@bogon appium_Demo1 % pip install Appium-Python-Client
Collecting Appium-Python-Client
Downloading appium_python_client-5.1.2.tar.gz (8.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.1/8.1 MB 42.8 kB/s eta 0:00:00
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
安装成功提示:
Successfully built Appium-Python-Client
Installing collected packages: sortedcontainers, websocket-client, urllib3, typing_extensions, sniffio, pysocks, idna, h11, certifi, attrs, wsproto, outcome, trio, trio-websocket, selenium, Appium-Python-Client
Successfully installed Appium-Python-Client-5.1.2 attrs-25.3.0 certifi-2025.7.14 h11-0.16.0 idna-3.10 outcome-1.3.0.post0 pysocks-1.7.1 selenium-4.34.2 sniffio-1.3.1 sortedcontainers-2.4.0 trio-0.30.0 trio-websocket-0.12.2 typing_extensions-4.14.1 urllib3-2.5.0 websocket-client-1.8.0 wsproto-1.2.0
[notice] A new release of pip is available: 25.1.1 -> 25.2
[notice] To update, run: pip install --upgrade pip
如果提示更新 pip,想更新 pip,可以复制它给的复制命令,继续在此处更新即可。
(.venv) nhl@bogon appium_Demo1 % pip install --upgrade pip
Requirement already satisfied: pip in ./.venv/lib/python3.13/site-packages (25.1.1)
Collecting pip
Downloading pip-25.2-py3-none-any.whl.metadata (4.7 kB)
Downloading pip-25.2-py3-none-any.whl (1.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 44.9 kB/s eta 0:00:00
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 25.1.1
Uninstalling pip-25.1.1:
Successfully uninstalled pip-25.1.1
Successfully installed pip-25.2
安装成功就可以使用了,如果联想不出来,python文件手动导入
from appium import webdriver,不报红就可以了。
# # 初始化 driver
driver = webdriver.Remote(
"http://127.0.0.1:4723",
options = UiAutomator2Options().load_capabilities(caps)
)
录制的测试脚本可运行代码 python,在 python 脚本文件中,右键运行:
注意一定要确保:appium 启动、模拟器启动,可看到运行脚本,模拟器自动按照测试脚本测试。
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
#设置 cpability
caps = {
# 设置 app 安装的平台(Android,iOS)
"platformName": "Android",
# 设置 appium 驱动
"appium:automationName": "uiautomator2",
# 设置设备名称
"appium:deviceName": "127.0.0.1:62001",
# 设置被测 app 的包名
"appium:appPackage": "io.appium.android.apis",
# 设置被测 app 启动页面的 Activity
"appium:appActivity": ".ApiDemos"
}
# # 初始化 driver
driver = webdriver.Remote(
"http://127.0.0.1:4723",
options = UiAutomator2Options().load_capabilities(caps)
)
# 测试步骤
# 找到 0s 元素
el1 = driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="OS")
# 点击 os 元素
el1.click()
# 找到 Morse Code 元素
el2 = driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="Morse Code")
# 点击Morse Code 元素
el2.click()
# 找到输入框元素
el3 = driver.find_element(by=AppiumBy.ID, value="io.appium.android.apis:id/text")
# 在输入框中输入内容
el3.send_keys("测试人")
# 点击返回按钮
driver.back()
# 点击返回按钮
driver.back()
注:此脚本只是 inspector 录制基础脚本,想成为真正的测试用例脚本还需要优化:
- 添加
setup_method和teardown_mehtod - 添加
断言













网友评论