Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to identify xpath for android element using appium

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.

This is how the screen looks like in UI Automator Viewer: enter image description here

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.

http://www.zvon.org/comp/r/tut-XPath_1.html#intro

http://www.w3.org/TR/xpath/

Would appreciate any help.

like image 289
Ofir A. Avatar asked Oct 02 '14 11:10

Ofir A.


People also ask

How can check Xpath is correct or not in Appium?

If you use the UI “Desktop” version of Appium, it has an inspector that you can enter xpath and other locator strategies into and will highlight the page when it finds it.

How will you identify elements in Appium?

Finding elements using the ID is, by far the simplest technique. Each element has a unique ID assigned to it that helps in identifying and interacting with it. Appium has a Native element identifier for Android and iOS. resource-id is used as an element identifier for Android and name is used for iOS.

How do I find DOM element or Xpath in mobile app?

To find the DOM element use "UIAutomateviewer" to find DOM element for Android application. Connect your Phone or Emulator with Appium desktop client. Start Inspect Session. Start session by specifying capabilities for device - deviceName, platformName, automationName, appPackage, appActivity.


2 Answers

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

  1. You can't search by root nodes.. link
    • Unfortunately, this means that a verbose xpath is likely the xpath(pun) to success
  2. 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 RelativeLayouts and then do something like:

//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].

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)

like image 122
Jess Avatar answered Oct 07 '22 16:10

Jess


Just try this u can access easily

driver.findElement(By.xpath("//*[@class='android.widget.FrameLayout' and @bounds='[418,564][780,885]']")).click();

Screenshot of UI

like image 44
Rajesh G Avatar answered Oct 07 '22 17:10

Rajesh G