You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Appium-Python-Client元素定位故障求助:两种方法均失效

解决Appium Android元素定位问题

问题根源

你遇到的问题是Appium Python Client v2.x已弃用find_element_by_id这类旧版定位方法,同时使用find_element时参数格式错误,导致方法无法生效。

解决方案

1. 确保安装最新版Appium Python Client

执行命令更新或安装依赖:

pip install --upgrade appium-python-client

2. 正确使用find_element方法

以下三种写法均可正常生效,按需选择:

写法一:使用AppiumBy常量(推荐)

先导入AppiumBy类,再结合元素ID定位:

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

desired_cap = {
  "appium:deviceName": "emulator-5554",
  "appium:appActivity": ".LandingScreen",
  "platformName": "Android",
  "appium:appPackage": "com.myapp",
  "appium:noReset": "true"
}

driver = webdriver.Remote("http://localhost:4723/wd/hub", desired_cap)
# 正确的元素定位代码
driver.find_element(AppiumBy.ID, "com.talabat:id/ivToolbarAddressArrow")

写法二:使用Selenium的By常量

若习惯Selenium的语法,可导入By类实现定位:

from appium import webdriver
from selenium.webdriver.common.by import By

# (desired_cap和driver初始化代码同上)
driver.find_element(By.ID, "com.talabat:id/ivToolbarAddressArrow")

写法三:直接使用字符串定位策略

无需导入额外类,直接用"id"作为定位策略:

from appium import webdriver

# (desired_cap和driver初始化代码同上)
driver.find_element("id", "com.talabat:id/ivToolbarAddressArrow")

3. 额外验证项

  • 确认元素ID准确:用Appium Inspector获取元素真实ID,避免拼写错误(如包名前缀、字符大小写)。
  • 检查Appium服务状态:确保http://localhost:4723/wd/hub可正常访问,设备/模拟器已正确连接。

内容的提问来源于stack exchange,提问作者Abdullah M. Adil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 06:55:27