Web应用程序中的Appium:无法在通知弹出窗口中点击允许权限按钮

Joh*_*Pix 3 java testing selenium automation appium

当我打开Web应用程序时,我会弹出一个窗口。我试图通过两种方式单击“允许”按钮:

1)当我添加权限时:

caps.setCapability("autoGrantPermissions", true);
caps.setCapability("autoAcceptAlerts", true);
......
driver.switchTo().alert().accept();
Run Code Online (Sandbox Code Playgroud)

没发生什么事((

2)当我尝试通过XPath查找它时:

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

我收到一个错误:

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

这是我从UI Automator Viewer截取的屏幕截图:

在此处输入图片说明

我发现了这篇文章:在Appium中点击许可警报的“允许”按钮后,无法点击链接? 但这对我没有帮助。

小智 6

第一:功能您试图使用是仅适用于iOS

在Android上,您必须通过查找弹出窗口findElement自行关闭它们

第二:自从启动Web应用程序的 Appium会话以来,在搜索本机弹出窗口之前,必须切换上下文

    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();
Run Code Online (Sandbox Code Playgroud)

不要忘记将上下文切换回Web以继续:

    driver.context(webContext);
Run Code Online (Sandbox Code Playgroud)