Appium in Web app: Unable to tap Allow permission button in notification pop up window

JohnPix :

When I am opening the web app, I am getting a pop up. I'm trying to click on 'ALLOW' button in two ways:

1) When I add permissions:

caps.setCapability("autoGrantPermissions", true);
caps.setCapability("autoAcceptAlerts", true);
......
driver.switchTo().alert().accept();

nothing happens((

2) When I try to find it by XPath:

driver.findElement(By.xpath("//android.widget.Button[@text='Allow']")).click();

I get an error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//android.widget.Button[@text='Allow']"}

This is my screenshot from UI Automator Viewer :

enter image description here

I found this post:Unable to tap the link after tap on Allow button of permission alert in Appium? but it didn't help me.

dmle :

First: capabilities you were trying to use are for IOS only

On Android you have to find popup via findElement and close them yourself.

Second: since you start Appium session for web application, before searching for native popups you must switch the context:

    String webContext = driver.getContext();
    Set<String> contexts = driver.getContextHandles();
    for (String context: contexts){
        if (context.contains("NATIVE_APP")){
            driver.context(context);
            break;
        }
    }
    driver.findElement(By.id("android:id/button1")).click();

Don't forget to switch context back to web to continue:

    driver.context(webContext);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=86027&siteId=1