appium get_attribute 方法的坑

在Appium中,使用get_attribute方法获取元素的content-desc属性时遇到NoSuchElement错误。问题源于bootstrap处理attribute的方式,特别是对于'content-desc'的处理。解决方案和相关源码分析有助于理解并避免此类问题。
摘要由CSDN通过智能技术生成

目录

前言:

问题描述

问题原因探究


前言:

在 Appium 中,get_attribute 方法是用来获取元素属性的方法。它可以用来获取元素的文本、颜色、位置等属性。

问题描述

当使用类似下面的代码获取元素的 content-desc 属性时,会报 NoSuchElement 错误:

# python
self.driver.find_element_by_id("id").get_attribute("content-desc")

但使用如下代码却能正常执行:

# python
self.driver.find_element_by_id("id").click()

很明显,这个错误原因不是找不到元素,而是 get_attribute 出问题。

问题原因探究

appium server 在 android 原生应用上获取 attribute 的大致流程为:

  1. 从 client 收到获取 attribute 的请求
  2. 把请求转发给在手机上运行的 bootstrap
  3. bootstrap 调用相关方法进行实际操作
  4. bootstrap 返回结果给 appium server
  5. appium server 把结果返回给 client

通过排查各部分的代码发现,错误是在 bootstrap 产生的(排查过程涉及代码有点多,所以这里就不解释了),所以看看 bootstrap 相关源码:

lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/GetAttribute.java

@Override
  public AndroidCommandResult execute(final AndroidCommand command)
      throws JSONException {
    if (command.isElementCommand()) {
      // only makes sense on an element
      final Hashtable<String, Object> params = command.params();

try {

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值