How to determine the XPATH of Android UI elements.

2019-06-23 23:28发布

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!

8条回答
老娘就宠你
2楼-- · 2019-06-24 00:00
com.android2.calculator3:id/digit8

For Elements with 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
查看更多
欢心
3楼-- · 2019-06-24 00:01

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.

查看更多
姐就是有狂的资本
4楼-- · 2019-06-24 00:03

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.

查看更多
Anthone
5楼-- · 2019-06-24 00:06

Appium's client comes with an Inspector tool! This will tell you the xpath of whatever element you're interested in

查看更多
Animai°情兽
6楼-- · 2019-06-24 00:06

You could use it like this:

WebElement btn_yes = driver.findElement(By.xpath("//android.widget.Button[@text='Yes']"));
查看更多
我只想做你的唯一
7楼-- · 2019-06-24 00:07

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.

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.

查看更多
登录 后发表回答