网站管理助手 mysql,织梦网站上传到服务器,怎样做科技小制作视频网站,网站 水印下面是一个完整的 Appium WebDriver 支持的常用方法汇总#xff0c;并附上典型用法示例。 一、元素查找方法/元素操作方法
✅ 使用 find_element() 和 find_elements()
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy# 单个元素查找
…下面是一个完整的 Appium WebDriver 支持的常用方法汇总并附上典型用法示例。 一、元素查找方法/元素操作方法
✅ 使用 find_element() 和 find_elements()
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy# 单个元素查找
el driver.find_element(byAppiumBy.ID, valuecom.example:id/username)# 多个元素查找
els driver.find_elements(byAppiumBy.CLASS_NAME, valueandroid.widget.EditText)
✅ 支持的定位方式AppiumBy
定位方式示例值AppiumBy.IDcom.example:id/btn_loginAppiumBy.CLASS_NAMEandroid.widget.EditTextAppiumBy.NAMElogin_button已不推荐AppiumBy.XPATH//android.widget.TextView[textSubmit]AppiumBy.ACCESSIBILITY_IDbutton_loginAppiumBy.ANDROID_UIAUTOMATORnew UiSelector().resourceId(com.example:id/login)AppiumBy.IOS_PREDICATElabel LoginiOS 专用AppiumBy.IOS_CLASS_CHAINXCUIElementTypeButton[1]iOS 专用 举例说明常用的 Appium 定位方式配合 Appium Inspector 使用
我们以一个假设的登录页面为例其中包含以下 UI 元素信息通过 Appium Inspector 查看
属性值用户名输入框resource-idcom.example:id/username密码输入框classandroid.widget.EditText登录按钮textLogin 或 content-desclogin_button发送按钮resource-idcom.example:id/send提交按钮xpath//android.widget.Button[textSubmit] ✅ 示例 1通过 ID 定位推荐
from appium.webdriver.common.appiumby import AppiumBy# 定位发送按钮
send_button driver.find_element(byAppiumBy.ID, valuecom.example:id/send)
send_button.click() ✅ 示例 2通过 XPath 定位灵活但效率略低
submit_button driver.find_element(byAppiumBy.XPATH,value//android.widget.Button[textSubmit]
)
submit_button.click() ✅ 示例 3通过 Class Name 定位适用于多个同类型元素
edit_text_list driver.find_elements(byAppiumBy.CLASS_NAME,valueandroid.widget.EditText
)# 输入用户名到第一个 EditText
edit_text_list[0].send_keys(testuser) ✅ 示例 4通过 Accessibility IDcontent-desc定位
login_button driver.find_element(byAppiumBy.ACCESSIBILITY_ID,valuelogin_button
)
login_button.click() ✅ 示例 5通过 Text 定位注意大小写和空格
login_button driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().text(Login)
)
login_button.click() ✅ 示例 6模糊匹配文本contains
button driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().textContains(Sub)
)
button.click() ✅ 示例 7通过 resource-id text 组合定位更稳定
element driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().resourceId(com.example:id/send).text(Send Message)
)
element.click() ✅ 使用new UiSelector()语法定位元素
在 Appium 自动化测试中特别是在 Android 平台上new UiSelector() 是一种非常强大且灵活的定位元素方式。它属于 Android UI Automator 提供的 API允许你通过多种属性组合来精确定位页面上的控件。 1.什么是 new UiSelector()
UiSelector 是 Android UI Automator 提供的一个类。在 Appium 中可以通过 AppiumBy.ANDROID_UIAUTOMATOR 来使用它。它可以结合多个属性如 text, resourceId, className, index, description 等进行查找适合处理复杂的 UI 结构。
2.基本语法结构
from appium.webdriver.common.appiumby import AppiumByelement driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().method(value)
) ⚠️ 注意传入的是一个字符串表达式语法要严格符合 Java 的写法。 3.常用方法详解与示例
✅ 1. text(String text)
根据文本内容定位控件。
driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().text(Login)
) 适用于按钮、TextView 等显示文字的控件。 ✅ 2. textContains(String substr)
模糊匹配文本内容包含子串
driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().textContains(Log)
) ✅ 3. textStartsWith(String prefix)
以某个前缀开头的文本
driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().textStartsWith(Wel)
) ✅ 4. textMatches(Pattern regex)
正则匹配文本内容
driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().textMatches(^User\\d$) # 匹配 User123
) ✅ 5. resourceId(String id)
通过资源 ID 定位控件推荐用 AppiumBy.ID 更简洁
driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().resourceId(com.example:id/username)
) ✅ 6. className(String className)
通过类名定位控件如 EditText, Button
driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().className(android.widget.Button)
) ✅ 7. description(String contentDescription)
通过 content-desc 属性定位Accessibility ID
driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().description(menu_icon)
) ✅ 8. index(int index)
通过索引定位慎用容易变化
# 找第一个 TextView
driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().className(android.widget.TextView).index(0)
) ✅ 9. instance(int instance)
获取某一类控件中的第几个实例类似 XPath 中的 [n]
# 获取第二个 EditText
driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().className(android.widget.EditText).instance(1)
) ✅ 10. 组合使用多个条件
你可以组合多个条件来更精确地定位元素
# 同时匹配 resourceId 和 text
driver.find_element(byAppiumBy.ANDROID_UIAUTOMATOR,valuenew UiSelector().resourceId(com.example:id/login).text(Login)
) ✅ 11.注意事项
项目说明性能比 ID 和 XPath 略慢但比 XPath 更稳定推荐用途复杂控件、动态内容、没有唯一 ID 的场景调试建议使用 Appium Inspector 查看控件属性只能在 Android 上使用不支持 iOSiOS 需要用 IosUIAutomation 或 Predicate String ✅ 12.常见问题解答
❓ Q1为什么找不到元素
控件还没加载出来加 time.sleep() 或显式等待文本大小写不一致注意区分控件是动态生成的尝试换用其他属性使用 driver.page_source 查看当前页面结构。
✅ 13.常用定位方式对比
定位方式示例值特点AppiumBy.IDcom.example:id/send推荐使用速度快唯一性强AppiumBy.XPATH//android.widget.Button[textSubmit]灵活但慢适合结构化定位AppiumBy.CLASS_NAMEandroid.widget.EditText多个元素时需结合索引AppiumBy.ACCESSIBILITY_IDlogin_button依赖无障碍描述iOS 和 Android 都支持AppiumBy.ANDROID_UIAUTOMATORnew UiSelector().text(Login)Android 专属强大但语法复杂 ✅ element支持的方法
在使用 Appium 定位到元素之后除了 click() 动作之外还有许多其他动作可以对这些元素执行。以下是几种常见的操作方法
1. 输入文本
send_keys()向输入框中输入文本。 element driver.find_element(byAppiumBy.ID, valuecom.example:id/username)
element.send_keys(testuser)
2. 清除文本
clear()清除输入框中的现有文本。 element driver.find_element(byAppiumBy.ID, valuecom.example:id/username)
element.clear()
3. 获取文本
text 属性获取元素显示的文本内容。 element driver.find_element(byAppiumBy.ID, valuecom.example:id/message)
print(element.text)
4. 检查元素是否可见或启用
is_displayed()判断元素是否对用户可见。is_enabled()检查元素是否可用例如按钮是否可点击。 element driver.find_element(byAppiumBy.ID, valuecom.example:id/loginButton)
if element.is_displayed() and element.is_enabled():element.click()
5. 获取属性值
get_attribute(name)获取元素的特定属性值。 element driver.find_element(byAppiumBy.ID, valuecom.example:id/username)
attribute_value element.get_attribute(name) # 或者 content-desc, resource-id 等
6. 提交表单
submit()对于某些支持提交的元素如表单可以调用此方法来提交。 form_element driver.find_element(byAppiumBy.ID, valuecom.example:id/loginForm)
form_element.submit()
7. 触摸手势
虽然不是直接作用于元素的方法但你可以通过 TouchAction 类对特定元素执行触摸手势
长按、轻扫等。 from appium.webdriver.common.touch_action import TouchActionaction TouchAction(driver)
action.long_press(elelement).wait(1000).release().perform()
8. 元素等待
有时候你可能需要等待某个元素变为可见或者存在
显式等待 from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECwait WebDriverWait(driver, 10)
element wait.until(EC.visibility_of_element_located((AppiumBy.ID, com.example:id/username)))
9. 获取元素大小和位置
size 属性 和 location 属性分别用于获取元素的尺寸和屏幕上的位置。 element driver.find_element(byAppiumBy.ID, valuecom.example:id/username)
print(Size:, element.size)
print(Location:, element.location)
10. 截图
尽管不是直接针对元素的方法但你可以截取整个屏幕的截图然后根据需要裁剪出特定元素的部分
driver.save_screenshot(screenshot.png)保存当前屏幕截图。 二、移动端专属方法来自 MobileCommand
这些方法只能在 Appium 中使用Selenium 不支持。
✅ 1. 滑动屏幕Swipe
driver.swipe(start_x100, start_y800, end_x100, end_y200, duration800)
✅ 2. 拖拽Drag and Drop
driver.drag_and_drop(origin_el, destination_el)
✅ 3. 按下和释放Touch Action
from appium.webdriver.common.touch_action import TouchActionaction TouchAction(driver)
action.press(x100, y500).wait(1000).release().perform() 更推荐使用新版的 W3CActions 方式。 ✅ 4. 启动应用 / 关闭应用
driver.activate_app(com.example.app) # 启动应用
driver.background_app(5) # 将应用置于后台运行5秒
driver.terminate_app(com.example.app) # 强制停止应用
✅ 5. 获取当前应用信息
print(Current Package:, driver.current_package)
print(Current Activity:, driver.current_activity)
✅ 6. 安装 / 卸载应用
driver.install_app(/path/to/app.apk)
driver.remove_app(com.example.app)
✅ 7. 判断是否已安装应用
if driver.is_app_installed(com.example.app):print(App is installed.)
✅ 8. 切换上下文WebView / Native
contexts driver.contexts
print(Available contexts:, contexts)driver.switch_to.context(contexts[-1]) # 切换到 WebView
✅ 9. 获取系统时间
print(Device time:, driver.device_time)
✅ 10. 发送键事件如返回、菜单
driver.press_keycode(4) # 返回键 KEYCODE_BACK
✅ 11. 截图
driver.save_screenshot(screen.png) 三、浏览器相关方法Hybrid App 或 Webview
如果你是在 WebView 中进行操作可以使用类似 Selenium 的方式
driver.get(https://example.com)
print(Current URL:, driver.current_url)
driver.back()
driver.forward() 四、键盘与输入操作
el.send_keys(Hello World)
el.clear()
如果遇到中文输入问题可设置以下 Desired Capabilities
dict: {resetKeyboard: True,unicodeKeyboard: True
} 五、截图与文件操作
driver.save_screenshot(test.png) # 保存截图
driver.get_screenshot_as_base64() # 获取 base64 编码图片 六、网络与设备状态
driver.is_locked() # 是否锁屏
driver.unlock() # 解锁
driver.lock(5) # 锁屏5秒后自动解锁
driver.set_network_connection(6) # 设置网络连接类型 七、执行脚本JavaScript、Driver Command
result driver.execute_script(mobile: scroll, {direction: down})
你也可以直接调用底层命令
driver.execute(mobile: longClickGesture, {x: 100,y: 100,duration: 1000
}) 八、会话控制
driver.quit() # 结束当前会话
driver.close_app() # 关闭当前应用但不结束会话 九、获取日志Logcat 等
logs driver.get_log(logcat)
for log in logs:print(log)
支持的日志类型包括
logcatbugreportserverdriverclient 十、完整方法列表部分
你可以通过以下方式查看所有可用的方法
dir(driver)
或者访问官方文档 Appium-Python-Client 文档 ✅ 总结常用方法速查表
功能方法查找元素find_element, find_elements触摸操作swipe, drag_and_drop, press_keycode应用控制activate_app, terminate_app, background_app安装卸载install_app, remove_app, is_app_installed上下文切换switch_to.context()输入文本send_keys, clear截图save_screenshot, get_screenshot_as_base64执行脚本execute_script, execute日志获取get_log(logcat)锁屏操作is_locked, unlock, lock 如果你有具体需求或想了解某个功能的详细用法欢迎告诉我我可以为你提供代码示例