Appium - Unable to locate android element using xpath

Viewed 716

I have to click the ALLOW, to give the app permissions on Android 7.0.

I Tried the following xpath

//android.widget.Button[contains(@resource-id,'com.android.packageinstaller:id/permission_allow_button')]

also tried //android.widget.Button[@text='ALLOW']

Getting error :

No Such element exception.not_able_to_locate_using_xpath

3 Answers

You can try it:

driver.findElement(By.id("com.android.packageinstaller:id/permission_allow_button")).click();

or

driver.findElement(By.Name("Allow")).click();

or

driver.findElement(new By.ByName("Allow")).click();

It's working fine for me.

Since, App requesting for permission. You can allow permission directy using Desired Capabilities. Try this:

DesiredCapabilities cap = new DesiredCapabilities(); 
cap.setCapability("autoGrantPermissions", "true");

Check this also.

First of all, you need to locate the alert, whether it is there or not? So what you can do you is you can create the list, which will check whether alert is present or not? And then if the alert is present, it will click on ALLOW button.

I am using PageObjectFactory Pattern so the following is my code which works fine for me:

@AndroidFindBy(xpath = "//android.widget.Button[@text='ALLOW']")
private List<MobileElement> alert;
@AndroidFindBy(xpath = "//android.widget.Button[@text='ALLOW']")
private MobileElement allowAlert;

 if (!alert.isEmpty()) {
        try {
            // do something
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        waitForElement(appiumDriver, allowAlert).click();
    }
Related