问题描述
当我打开网络应用程序时,我收到一个弹出窗口.我试图通过两种方式点击允许"按钮:
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 :
我找到了这个帖子:在 Appium 中点击权限提醒的允许按钮后无法点击链接?但这对我没有帮助.
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.
推荐答案
第一:功能您尝试使用的是 仅限IOS
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();
不要忘记将上下文切换回网络以继续:
Don't forget to switch context back to web to continue:
driver.context(webContext);
这篇关于Web 应用程序中的 Appium:无法在通知弹出窗口中点击允许权限按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!