Appium 【已解决】getAttribute 方法为何不支持参数为 index 呢?

tom_ma · 2014年07月30日 · 最后由 Archer 回复于 2014年08月01日 · 2404 次阅读

请教下,

WebElement Image = driver.findElementByXPath("//android.widget.ImageView[@index=0]");
System.out.println("get index Attribute"+ Image.getAttribute("index"));
System.out.println("get index Attribute"+ Image.getAttribute("clickable"));

Image.click();

为何 WebElement Image= driver.findElementByXPath("//android.widget.ImageView[@index=0]");
可以定位到元素,但是获取 Image.getAttribute("index") 这个属性就不支持呢,index 不是也是一种属性吗?

而 Image.getAttribute("clickable") 是能拿到是否可点击的数据。

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 70 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

共收到 7 条回复 时间 点赞

你这个问题最好描述的清楚一下,我怎么感觉问题不是在 index 上,index 是一个动态值,你最后的报错是说找不到这个元素,不是说 attribute 错误

发帖看下发帖的帮助啊,别把代码发的和文本一样

@qddegtya @lihuazhang 谢谢两位的回复,我把细节贴出来给大家看下:

WebElement Image = driver.findElementByXPath("//android.widget.ImageView[@index=0]");
System.out.println("get clickable Attribute = "+ Image.getAttribute("clickable"));
System.out.println("get index Attribute = "+ Image.getAttribute("index"));
Image.click();

Eclipse console 最后的输出是:get clickable Attribute = true,然后就是报错:
org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
而注释掉 System.out.println("get index Attribute = "+ Image.getAttribute("index"));这行代码的话,后面的 Image.click(); 是可以执行到,所以定位下来是判断这句代码出现的问题。

下面的是 appium 的日志:

POST /wd/hub/session/00b8dc7c-8d66-4bd4-b133-a80f21ada477/element 200 153ms - 109b
ERROR: debug: Appium request initiated at /wd/hub/session/00b8dc7c-8d66-4bd4-b133-a80f21ada477/element/9/attribute/clickable
info: Pushing command to appium work queue: ["element:getAttribute",{"elementId":"9","attribute":"clickable"}]
ERROR: debug: Request received with params: {}
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"element:getAttribute","params":{"elementId":"9","attribute":"clickable"}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: getAttribute
info: [BOOTSTRAP] [info] Returning result: {"value":"true","status":0}
info: Responding to client with success: {"status":0,"value":"true","sessionId":"00b8dc7c-8d66-4bd4-b133-a80f21ada477"}
ERROR: debug: Appium request initiated at /wd/hub/session/00b8dc7c-8d66-4bd4-b133-a80f21ada477/element/9/attribute/index
GET /wd/hub/session/00b8dc7c-8d66-4bd4-b133-a80f21ada477/element/9/attribute/clickable 200 20ms - 91b
info: Pushing command to appium work queue: ["element:getAttribute",{"elementId":"9","attribute":"index"}]
ERROR: debug: Request received with params: {}
info: [BOOTSTRAP] [info] Got data from client: {"cmd":"action","action":"element:getAttribute","params":{"elementId":"9","attribute":"index"}}
info: [BOOTSTRAP] [info] Got command of type ACTION
info: [BOOTSTRAP] [debug] Got command action: getAttribute
info: [BOOTSTRAP] [info] Returning result: {"value":"This element does not have the 'index' attribute","status":7}
info: Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"This element does not have the 'index' attribute"},"sessionId":"00b8dc7c-8d66-4bd4-b133-a80f21ada477"}

从 appium 的日志可以看出,是说 This element does not have the 'index' attribute,但是 driver.findElementByXPath("//android.widget.ImageView[@index=0]") 定位到了该元素,而且我用 uiatomatorview 能看到对应的 index 属性的哈,见截图:

所以完全搞不懂怎么回事了,可能帖子的标题没描述清楚,抱歉哈。

另外我尝试了下 TextView 对象,通过 text.getAttribute("index") 也是报这个错,难道 selunium 封装这个方法的时候,就不支持 index 参数的传入吗?

那他就是没实现嘛。。。

@lihuazhang 没想到,呵呵

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册