Web应用程序中的Appium:无法在通知弹出窗口中点击“允许"按钮 [英] Appium in Web app: Unable to tap Allow permission button in notification pop up window
问题描述
当我打开Web应用程序时,我会弹出一个窗口.我试图通过两种方式单击允许"按钮:
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)当我添加权限时:
1) When I add permissions:
caps.setCapability("autoGrantPermissions", true);
caps.setCapability("autoAcceptAlerts", true);
......
driver.switchTo().alert().accept();
什么都没发生((
2)当我尝试通过XPath查找它时:
2) When I try to find it by XPath:
driver.findElement(By.xpath("//android.widget.Button[@text='Allow']")).click();
我得到一个错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//android.widget.Button[@text='Allow']"}
这是我从UI Automator Viewer中截取的屏幕截图:
This is my screenshot from UI Automator Viewer :
我发现了这篇文章: 但这对我没有帮助.
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.
推荐答案
First: capabilities you were trying to use are for IOS only
在Android上,您必须通过findElement
找到弹出窗口,然后自行关闭弹出窗口.
On Android you have to find popup via findElement
and close them yourself.
第二:由于您启动了网络应用的Appium会话,因此在搜索本机弹出窗口之前,您必须切换上下文:
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();
别忘了将上下文切换回Web继续:
Don't forget to switch context back to web to continue:
driver.context(webContext);
这篇关于Web应用程序中的Appium:无法在通知弹出窗口中点击“允许"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!