Android Xpaths varying

Hi…
I have written my test cases to Automate an app in android mobile.few xpaths are varying from version to version.Say for few elements xpaths are little bit differingng from Lollypop and Marshmallow.By any chance am i able to handle like if Lollypop use, “XYZ” Xpath and if Marshmallow use “ABC” Xpath?Please help. Am i able to check in my test case if lollypop connects,use one path and if marshmallow, use different Xpath.Is it possible to do?
Please any one help me

Thanks,
Vani

@vani.hosagoudra : The best way is to use relative xpath instead of absolute. If you use relative, then xpath will work irrespective of device screen size and version. Alternatively, you can use the below code -

if(driver.getCapabilities().getCapability(“CapabilityType.VERSION”).toString().contains(“6”)) {
// YOUR-MARSHMALLOW-TEST-HERE
}

If you can post xpath, then I can give you solution to use relative one.

Thanks Soo much for the reply Suman_Bala.Here is one sample xpath of an element(Hamberg).
HambergMenu1=xpath~//android.widget.LinearLayout[1]/ android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/ android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/ android.support.v4.widget.DrawerLayout[1]/android.widget.LinearLayout[1]/ android.widget.FrameLayout[1]/android.view.View[1]/android.widget.ImageButton[1]
(Above xpath is for Lollypop 4.4)
HambergMenu=xpath~//android.widget.LinearLayout[1]/ android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/ android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/ android.support.v4.widget.DrawerLayout[1]/android.widget.LinearLayout[1]/ android.widget.FrameLayout[1]/android.view.ViewGroup[1]/ android.widget.ImageButton[1]
(This above xpath is for 6.0 Marshmallow.)
Please can you look into it.

Thanks,
Vani

As the only difference is android.view.View and android.view.viewGroup so you can use the below code. But make sure, your app uses android.view.View for android version 5 as well. If not, then chance the condition accordingly.

   if(driver.getCapabilities().getCapability("CapabilityType.VERSION").toString().contains("6")) {
        driver.findElement(By.xpath("//android.view.ViewGroup[1]/android.widget.ImageButton[1] ")).click();
    }
    else {
        driver.findElement(By.xpath("//android.view.View[1]/android.widget.ImageButton[1]")).click();
    }

thanks soo much Suman for the quick reply

i will try this. and let u know

@Suman_Bala, I am facing similar issue,
driver.findElement(By.xpath("//android.view.ViewGroup[1]/android.widget.ImageButton[1] ")).click();

All these referes to the index 1 right, so if my index is 0, I should be using driver.findElement(By.xpath("//android.view.ViewGroup[0]/android.widget.ImageButton[0] ")).click();
I tried putting a 0 to refer to the first element, the element is never identifiable in Android 6.x
Any tips? Please help.

Regards,
Angel