Appium appium 的 xpath 用法咨询。

adfghzhang · 2015年03月12日 · 最后由 water 回复于 2015年03月12日 · 1445 次阅读


得知 xpath 的 index 是从 1 开始,但目前写的有问题,前来请教:
我想使用 xpath 的方式去获取框住地方的值,目前 xpath 是这样写的:String xpath = "//ListView[1]/LinearLayout[1]/LinearLayout[1]/LinearLayout[0]/TextView[1]"
这样直接抛异常。日志如下:

info: [debug] Pushing command to appium work queue: ["find",{"strategy":"xpath","selector":"//ListView[2]/LinearLayout[1]/LinearLayout[2]/LinearLayout[1]/TextView[2]","context":"","multiple":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//ListView[2]/LinearLayout[1]/LinearLayout[2]/LinearLayout[1]/TextView[2]","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding //ListView[2]/LinearLayout[1]/LinearLayout[2]/LinearLayout[1]/TextView[2] using XPATH with the contextId: multiple: false
info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":13}
info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command."},"sessionId":"7b958536-b344-4a6f-b4b4-195d2824cc86"}
请问这 xpath 必须写出其绝对路径才能正常使用么?
还望各位不吝赐教!

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 7 条回复 时间 点赞

String xpath = "//FrameLayout[1]/LinearLayout[1]/FrameLayout[1]/RelativeLayout[1]/View[1]/FrameLayout[1]/FrameLayout[1]/LinearLayout[1]/FrameLayout[2]/FrameLayout[1]/ScrollView[1]/LinearLayout[1]/LinearLayout[2]/LinearLayout[2]/ListView[2]/LinearLayout[1]/LinearLayout[2]/LinearLayout[1]/TextView[2]";
这写全了也还是一样报错

#1 楼 @adfghzhang 试试用 UiAuaomtor 的 UiSelector 定位一下。

#1 楼 @adfghzhang 另外,你的 Xpath 写错了,Xpath 的 Index 是从 1 开始的,而不是从 0 开始,所以你的 index 有一些应该 +1。

String xpath = "//ListView[1]/LinearLayout[1]/LinearLayout[2]/LinearLayout[1]/TextView[2]"

appium 版本是多少?

xpath 应该写 full class 名。
比如;
String xpath = "//android.widget.TextView"

还有你这个 TextView 的 text 属性里没有文本吗?
如果有还可以这样写:
String xpath = "//android.widget.TextView[contains(@text,'文本')]"

额,我觉得你需要的是系统学习一下 xpath 这门语言……
可以去维基百科看看:http://zh.wikipedia.org/wiki/XPath

xpath 说到底就是一个在 xml 文档中定位 xml 元素的语言。用 index 虽然方便,但适应性很弱(上面这么多层,某一层变了你就悲剧了)

@jinjun0620 多谢,应该是这个问题吧。我通过变通的方式解决了这个问题。还是靠坐标来比 xpath 靠谱

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