简体   繁体   English

无法使用 Appium 在 Android 中的权限对话框中单击“允许”按钮

[英]Can't click Allow button in permission dialog in Android using Appium

I am unable to tap on Deny or Allow buttons on the permissions dialog in Android using Appium+Java.我无法使用 Appium+Java 在 Android 中的权限对话框上点击拒绝或允许按钮。 Do I need to add any capabilities before going to tap on those buttons?在点击这些按钮之前,我需要添加任何功能吗? Below is the code:下面是代码:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName", "ASUS_Z00LD");
capabilities.setCapability("platformVersion", "6.0");
capabilities.setCapability("app","<AppPath>");
capabilities.setCapability("browserName", "");
AndroidDriver<MobileElement> driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        Thread.sleep(10000);
driver.findElement(MobileBy.id("permission_allow_button")).click();

Below is error in Eclipse console:以下是 Eclipse 控制台中的错误:

org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)

Below is the Appium log:以下是 Appium 日志:

info: [debug] Responding to client with success: {"status":0,"value":{"platform":"LINUX","browserName":"","platformVersion":"6.0.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","browserName":"","platformName":"Android","deviceName":"ASUS_Z00LD","platformVersion":"6.0"},"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","platformName":"Android","deviceName":"FAAZCY127084"},"sessionId":"b64fd5af-3de5-4299-a2d4-1948fc8e883e"}
info: <-- GET /wd/hub/session/b64fd5af-3de5-4299-a2d4-1948fc8e883e 200 1.534 ms - 625 {"status":0,"value":{"platform":"LINUX","browserName":"","platformVersion":"6.0.1","webStorageEnabled":false,"takesScreenshot":true,"javascriptEnabled":true,"databaseEnabled":false,"networkConnectionEnabled":true,"locationContextEnabled":false,"warnings":{},"desired":{"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","browserName":"","platformName":"Android","deviceName":"ASUS_Z00LD","platformVersion":"6.0"},"app":"/Users/Shiva/Documents/workspace/AndroidPractice/APK/****_Android.apk","platformName":"Android","deviceName":"FAAZCY127084"},"sessionId":"b64fd5af-3de5-4299-a2d4-1948fc8e883e"}

info: --> POST /wd/hub/session/b64fd5af-3de5-4299-a2d4-1948fc8e883e/element {"using":"id","value":"permission_allow_button"}

info: [debug] Waiting up to 0ms for condition

info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"permission_allow_button","context":"","multiple":false}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"permission_allow_button","context":"","multiple":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: find
info: [debug] [BOOTSTRAP] [debug] Finding permission_allow_button using ID with the contextId:  multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.****.****:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=android:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[DESCRIPTION=permission_allow_button, INSTANCE=0]

info: [debug] [BOOTSTRAP] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
info: [debug] [BOOTSTRAP] [debug] Finding permission_allow_button using ID with the contextId:  multiple: false
info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.****.****:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=android:id/permission_allow_button]

info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[DESCRIPTION=permission_allow_button, INSTANCE=0]

info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":7,"value":"No element found"}

info: [debug] Condition unmet after 178ms. Timing out.

info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"No element found"},"sessionId":"b64fd5af-3de5-4299-a2d4-1948fc8e883e"}
info: <-- POST /wd/hub/session/b64fd5af-3de5-4299-a2d4-1948fc8e883e/element 500 181.769 ms - 195 

Anyone's help to overcome this would be appreciated?任何人帮助克服这一点将不胜感激?

Use full resource ID ...It worked for me....使用完整的资源 ID ......它对我有用......

below line worked for me.... driver.findElement(MobileBy.id("com.android.packageinstaller:id/permission_allow_button")).click();下面的行对我有用.... driver.findElement(MobileBy.id("com.android.packageinstaller:id/permission_allow_button")).click();

With the below snippet I am able to click on all the allow buttons to get the permissions.使用以下代码段,我可以单击所有允许按钮以获取权限。

while (driver.findElements(MobileBy.xpath("//*[@class='android.widget.Button'][2]")).size()>0) {
    driver.findElement(MobileBy.xpath("//*[@class='android.widget.Button'][2]")).click();
}

Starting from appium 1.6.3 you can just add:从 appium 1.6.3 开始,您可以添加:

capabilities.setCapability("autoGrantPermissions", "true");

And you'll always allow all permissions your app wants.而且您将始终允许您的应用程序想要的所有权限。

Appium gives you an API that detect the activity. Appium 为您提供了一个检测活动的 API。 Depending upon your device, you could get two activities - the package name may get stripped off or not:根据您的设备,您可以获得两个活动 - 包名称可能会被删除:

'com.android.packageinstaller.permission.ui.GrantPermissionsActivity',
'.permission.ui.GrantPermissionsActivity'

After detecting this activity, you need to find an element by locator(id/xpath):检测到此活动后,您需要通过 locator(id/xpath) 查找元素:

'com.android.packageinstaller:id/permission_message'

Then you can obtain the text of that message if you are interested in it.如果您对此感兴趣,则可以获取该消息的文本。 If you care which permission it is, you can match it against expected strings or regular expressions.如果您关心它是哪个权限,您可以将它与预期的字符串或正则表达式进行匹配。 If not, you can blindly accept by finding and clicking the element by id:如果没有,你可以通过id查找并点击元素来盲目接受:

'com.android.packageinstaller:id/permission_allow_button'

If you'd rather not click 'allow' on all those windows, you can use adb to add all the permissions at once before you start testing (but after Appium has installed your app).如果您不想在所有这些窗口上单击“允许”,您可以在开始测试之前(但在 Appium 安装您的应用程序之后)使用 adb 一次性添加所有权限。 If you know all the perms your app will need, you can add them with one command:如果您知道应用程序需要的所有权限,则可以使用一个命令添加它们:

pm grant $app_name $space_delimited_set_of_perms

Or you can add all permissions one at a time, which takes 1.5-2 seconds per attempt.或者您可以一次添加所有权限,每次尝试需要 1.5-2 秒。

Reference : https://discuss.appium.io/t/android-m-and-permissions/5760/13参考: https : //discuss.appium.io/t/android-m-and-permissions/5760/13

I tried lot many solutions before I found what is the fix.在找到修复方法之前,我尝试了很多解决方案。 I had enabled Touch ball / quick ball feature and icon used stay always on the screen to help me navigate.我启用了触摸球/快速球功能,并且使用的图标始终显示在屏幕上以帮助我导航。

This app was the culprit which had overlay feature enabled and wasn't easy to figure it out by browsing the active apps under the installed apps.这个应用程序是启用覆盖功能的罪魁祸首,通过浏览已安装应用程序下的活动应用程序并不容易弄清楚。

Note: Mine was MIUI 11 with Android 7 for Redmi Note 4.注意:我的是 MIUI 11 和 Android 7,用于 Redmi Note 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中使用Appium无法单击设备的位置许可对话框“允许/拒绝”按钮 - Unable to click device's location permission dialog Allow/Deny button in Android using Appium 单击按钮后无法显示警报对话框 - Can't display alert dialog on button click Android 在Selenium Appium中授予“ com.android.packageinstaller:id / permission_allow_button”权限后无法找到定位器 - Unable To Find Locator After Giving Permission “com.android.packageinstaller:id/permission_allow_button” In Selenium Appium 无法单击使用Appium Java for Android应用未启用的按钮 - Not able to click a button which is not enabled using Appium Java for Android app 无法使用appium 1.6.4在android native应用中单击按钮 - Not able to click on button in android native app using appium 1.6.4 在Appium中点击权限警报的“允许”按钮后,无法点击链接? - Unable to tap the link after tap on Allow button of permission alert in Appium? 如何在Appium中允许系统弹出位置权限? - How can I allow system popup for location permission in Appium? 如何用Appium双击android后退按钮? - How to double click android back button with Appium? 无法显示android权限请求对话框 - Can't get the android permission request dialog to appear 无法在 Android (Appium) 中使用 xpaht 单击元素 - Unable to click on an element by using xpaht in Android (Appium)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM