如何确定 Android UI 元素的 XPATH。硒 + Appium
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24166821/
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
How to determine the XPATH of Android UI elements. Selenium + Appium
提问by IWantMoore
I am currently working on automating an android application using Appium and Selenium and have run into some problems with locating elements by ID. I want to use XPATH but do not know how to get the XPATH of android UI elements. Does anyone know how to determine the XPATH of an element or know of a tool that can do this work quickly? Thanks!
我目前正在使用 Appium 和 Selenium 自动化一个 android 应用程序,并且在通过 ID 定位元素时遇到了一些问题。我想使用 XPATH 但不知道如何获取 android UI 元素的 XPATH。有谁知道如何确定元素的 XPATH 或知道可以快速完成这项工作的工具吗?谢谢!
回答by user220448
You can use android sdk tool - uiautomatorviewer( https://developer.android.com/tools/testing/testing_ui.html) it's like appium inspector, but work better for me.
您可以使用 android sdk 工具 - uiautomatorviewer( https://developer.android.com/tools/testing/testing_ui.html) 它就像 appium 检查器,但对我来说效果更好。
回答by Kairat Kempirbaev
com.android2.calculator3:id/digit8
For Elements with id
对于带有 id 的元素
driver.findElement(By.id("com.example.testapp:id/txtLogin"))
For Elements with class
对于具有类的元素
android.widget.EditText[@index=0]
Syntax
句法
{ClassName}[@{anyAttributeName}={valueOfAttribute}]
Code
代码
driver.findElements(By.className("android.widget.EditText"));//will return ARRAY
回答by Vinay
There is a way you can figure out the xpath. First get the page source from the application. This will be in xml format. Then copy the source to a xpath tester website like xpath tester. Then create the xpath and check if it works. This is assuming you already know how to create xpaths.
有一种方法可以找出xpath。首先从应用程序获取页面源。这将是 xml 格式。然后将源复制到 xpath tester 网站,如xpath tester。然后创建 xpath 并检查它是否有效。这是假设您已经知道如何创建 xpath。
回答by Jess
Appium's client comes with an Inspector tool!This will tell you the xpath of whatever element you're interested in
Appium 的客户端自带 Inspector 工具!这将告诉您您感兴趣的任何元素的 xpath
回答by nwill001
Try viewing the source code:
尝试查看源代码:
https://android.stackexchange.com/questions/5039/how-do-i-view-source-in-the-android-browser
https://android.stackexchange.com/questions/5039/how-do-i-view-source-in-the-android-browser
Then figuring out the xpath becomes easier from there.
然后从那里找出 xpath 变得更容易。
回答by Aniish
Enable developer options in your phone and Connect your phone and invoke the chrome inspector for android devices( type -- about:inspect in chrome ) your device will be displayed.
在您的手机中启用开发人员选项并连接您的手机并调用 android 设备的 chrome 检查器(类型 -- about:inspect in chrome )您的设备将被显示。
Just click on the Inspect button & the screen in mobile will be displayed. You are provided with the application and the complete DOM structure.. Make your own XPATH from that.
只需单击“检查”按钮,就会显示移动设备中的屏幕。为您提供了应用程序和完整的 DOM 结构。从中制作您自己的 XPATH。
回答by Maharshi
You can use UIAutomatorviewer utility in Android SDK to get the required locator. Go to your Android SDK installation's 'tools' folder and trigger uiautomatorviewer.bat file. From there, you can locate app elements.
您可以使用 Android SDK 中的 UIAutomatorviewer 实用程序来获取所需的定位器。转到您的 Android SDK 安装的“工具”文件夹并触发 uiautomatorviewer.bat 文件。从那里,您可以找到应用程序元素。
Visit this link which shows how to use it.
访问此链接,其中显示了如何使用它。
回答by dqthebt
You could use it like this:
你可以这样使用它:
WebElement btn_yes = driver.findElement(By.xpath("//android.widget.Button[@text='Yes']"));