Search code examples
androidseleniumappiumappium-android

How to find a element xpath without Content-desc, resource Id or text attributes


Using Appium,

I want to click on an (arrow) button in the below picture. But the button has no resource-id, content-desc or text. So, is there any method by which we can click on the button. I am using java language for writing scripts to run. Thank you in advance:)

enter image description here


Solution

  • Use find element By.XPath:

    //*[@class='android.widget.Button'][1]
    

    The above for first android.widget.Button.

    If in the page there are android.widget.Button more than one, you can try to change sequence of [1].

    With java, you can collect them in list and create a condition to make sure there are button more than one or not:

    List<MobileElement> buttons = driver.findElements(By.xpath("//*[@class='android.widget.Button']"));
    
    MobileElement nextBtn;
    MobileElement backBtn;
    if(buttons.size()==1) {
        nextBtn = buttons.get(0);
    }else {
        nextBtn = buttons.get(1);
        backBtn = buttons.get(0);
    }
    
    //if you want click
    nextBtn.click();
    

    Note:

    Not sure how to you initialize the driver, also you can change MobileElement to AndroidElement