Xpath is different for tab and phone view for the same app

I have seen the xpath is different for tab and phone for the same app provided by uiautomatorviewer in android.
How to resolve this ?? please help

Yes, same issue I am also facing. Even I observed that this is for the different devices for the same app.

then any solution for the same??? any substitute??

@Jassi_Kaushik can you share a screenshot from both devices?


I am clicking the drawer icon. for the same , i want to use xpath. but unfortunality the xpath for tab is not same as in phone.
for example -> for the xpath of drawer icon, android.view.View is present in tab but for the same icon android.view.viewgroup is in the phone.

Any suggestions???
Below is for TAB:

Below is for Phone:

@Jassi_Kaushik have you tried something like By.xpath("//android.view.View | //android.view.viewgroup")
This should find either one if they are unique. Also, if you include a screenshot from uiautomatorviewer would be more helpful.

I am using strategy approach.
can you please how can i write the same with below respect???
@FindBy(xpath = “//android.widget.FrameLayout[@index=‘0’]/android.widget.LinearLayout[@index=‘0’]/android.widget.FrameLayout[@index=‘0’]/android.widget.LinearLayout[@index=‘0’]/android.widget.FrameLayout[@index=‘0’]/android.widget.LinearLayout[@index=‘0’]/android.support.v4.widget.DrawerLayout[@index=‘0’]/android.widget.LinearLayout[@index=‘0’]/android.widget.LinearLayout[@index=‘0’]**/android.view.View[@index=‘0’]/**android.widget.ImageButton[@index=‘0’]”)

@Jassi_Kaushik can you share the screenshots of uiautomator of both Smartphone and Tablet. It’s easier to identify which bit are actually necessary. From what you wrote I think you could use something like:

@FindBy(xpath = “//android.view.View[@index=‘0’]/android.widget.ImageButton[@index=‘0’] | //android.view.viewgroup[@index=‘0’]/android.widget.ImageButton[@index=‘0’]”)


@FindBy(xpath = “//android.view.View[@index=‘0’]/android.widget.ImageButton[@index=‘0’] | //android.view.viewgroup[@index=‘0’]/android.widget.ImageButton[@index=‘0’]”)

is not working, m getting no such element found expection.
please find below
for smartphone

for tablet

I could not read the content-description for the ImageButton, but you could use it. So, instead of adding the whole all the way to the button, you could just use this:
xpath(“//android.widget.ImageButton[@content-desc='Open navigation drawer']”) //Remeber to amend the text inside the content-desc, I’m not sure it contains Open navigation drawer
This should work for both. In worst case scenario, you can create the xpath locator as, xpath(“xpath1 | xpath2”).
Let me know if it worked

If u have more than one locator for the same element. U can use ByAll class like
new ByAll(By.xpath(“mobile xpath”), By.xpath(“tab xpath”).

in the views for example image button doesnt contain content-desc. hence couldnt use xpath(“//android.widget.ImageButton[@content-desc=‘Open navigation drawer’]”).

And thank you sooo much for the solution xpath(“xpath1 | xpath2”) . yes it works.

okey thanks :slight_smile: will surely use.

Could you more elaborate on this?

Create simple ByAll object.
private ByAll byAllExam = new ByAll(By.xpath(“xpath”),By.id(“id”),By.xpath(“xpath2”));

then findElement(byAllExam) or findElements(byAllExam) method of driver to locate single of multiple element.

For more detail regarding ByAll u can go throw the link.

1 Like

Hi all,
I am getting Exception in thread “main” org.openqa.selenium.WebDriverException: unknown error: Unsupported locator strategy: -android uiautomator when I try to use AndroidUIAutomator in my program

String nms=driver.findElementsByAndroidUIAutomator(“new UiSelector().className(“android.widget.Button”)”).toString();