需要使用appium识别android元素的xpath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26159603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need to identify xpath for android element using appium
提问by Ofir A.
I am testing my Android application using Appium framework. I have an android screen that doesn't have ids for its views (and I don't want to add...), so I thought using Xpath.
我正在使用 Appium 框架测试我的 Android 应用程序。我有一个没有视图 id 的 android 屏幕(我不想添加...),所以我想使用 Xpath。
This is how the screen looks like in UI Automator Viewer:
这是UI Automator Viewer 中屏幕的样子:
I want to get all the relative layouts (marked in red - sixteen items)
我想获取所有相关布局(以红色标记 - 十六项)
I tried the following:
我尝试了以下方法:
List<WebElement> webElementslist =
mAppDriver.findElementsByXPath("//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ViewAnimator[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[2]");
But I didn't get any items.
但我没有得到任何物品。
I searched the web and found the next xpath tutorials, tried more options, but again with no success.
我在网上搜索并找到了下一个 xpath 教程,尝试了更多选项,但还是没有成功。
http://www.zvon.org/comp/r/tut-XPath_1.html#intro
http://www.zvon.org/comp/r/tut-XPath_1.html#intro
Would appreciate any help.
将不胜感激任何帮助。
采纳答案by Jess
Right now there's a couple nasty bugs with XPath on android that explain the behaviors you're seeing. They are scheduled to be fixed in the 1.3.1 release
现在,Android 上的 XPath 存在一些令人讨厌的错误,它们可以解释您所看到的行为。它们计划在1.3.1 版本中修复
- You can't search by root nodes.. link
- Unfortunately, this means that a verbose xpath is likely the xpath(pun)to success
- You sometimes get 240 of an element when you only have 16 link
Ideally, you could look for the resource-id of the android.widget.LinearLayout
parent of all 16 RelativeLayout
s and then do something like:
理想情况下,您可以查找android.widget.LinearLayout
所有 16 个RelativeLayout
s的父级的资源 ID,然后执行以下操作:
//android.widget.LinearLayout[@resource-id="foo"]/android.widget.RelativeLayout
Your verbose solution did not work because you gave one of the layouts a position of [2]
.
您的详细解决方案不起作用,因为您为其中一个布局提供了[2]
.
Here it is, fixed:
这是固定的:
//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ViewAnimator[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.RelativeLayout
.... please, please use the first solution.
....拜托,请使用第一个解决方案。
I'm looking into a solution for your problem using UiAutomator
locator strategy, but no answers yet (because .fromChild()
and .fromParent()
seem to be broken)
我在找到你的问题的解决方案使用UiAutomator
定位策略,但没有解答(因为.fromChild()
和.fromParent()
似乎被打破)
回答by TinyTimZamboni
Keeping in mind the fact that //foo[1]
returns all foo nodes which are the first children of a their parent (not the first child of the foo node) try something like this:
请记住,//foo[1]
返回所有 foo 节点是其父节点的第一个子节点(不是 foo 节点的第一个子节点)的事实,请尝试以下操作:
//ScrollView/LinearLayout/LinearLayout[2]/LinearLayout/RelativeLayout
//ScrollView/LinearLayout/LinearLayout[2]/LinearLayout/RelativeLayout
Not tested, but it should give you all the RelativeLayouts which are a child of a LinearLayout which is a child of a LinearLayout which is the second child of a LinearLayout which is the child of a ScrollView.
未经测试,但它应该为您提供所有相对布局,它们是 LinearLayout 的子代,LinearLayout 是 LinearLayout 的子代,LinearLayout 是 ScrollView 的子代的 LinearLayout 的第二个子代。
回答by Rajesh G
Just try this u can access easily
试试这个你可以轻松访问
driver.findElement(By.xpath("//*[@class='android.widget.FrameLayout' and @bounds='[418,564][780,885]']")).click();
回答by Mat
The following code worked for me:
以下代码对我有用:
WebElement webElement = driver.findElementByXPath("//android.widget.ListView[1]/android.widget.LinearLayout[1]");