如何使用 Appium C# 处理 Android 权限对话
How to handle Android permission Dialogues using Appium C#
我正在将 Appium 与 C# 客户端一起使用。
启动 Android 应用程序时,我会看到几个像这样的权限对话框。
我想点击 "Allow" 或 "Deny" 按钮,但我无法使用以下代码执行此操作。
var Allow_Btn = driver.FindElementById("permission_allow_button");
Allow_Btn.Click();
经过大量谷歌搜索后,我发现我们可以使用以下功能跳过这些权限。
Capability.SetCapability("autoGrantPermissions", "true");
但是我想知道有没有其他方法可以做到。
提前致谢。
我不知道你从哪里得到这个 permission_allow_button
ID,我认为定位器不正确。
这是我在 Android Device Monitor application
中的样子
因此,如果您仍想使用 strategy of finding the element by ID - 您应该使用 com.android.packageinstaller:id/permission_allow_button
ID。
XPath selectors 的另一个选项是:
//Button[@text()='ALLOW]
//Button[@resource-id='com.android.packageinstaller:id/permission_allow_button']
您只能通过 Xpath 定位器来完成。按 ID 查找不适用于权限对话框处理。
要查找 "ALLOW" 按钮,您可以使用
driver.find_element_by_xpath("//*[@class='android.widget.Button'][2]") //it denotes "Allow" button
为了"DENY"按钮你,
driver.find_element_by_xpath("//*[@class='android.widget.Button'][1]") //it denotes "Deny" button
在点击按钮之前,您应该等到找到元素
WebDriverWait wait = new WebDriverWait(driver, timeOut);
wait.until(ExpectedConditions.presenceOfElementLocated(element));
希望对您有所帮助。
我正在将 Appium 与 C# 客户端一起使用。
启动 Android 应用程序时,我会看到几个像这样的权限对话框。
我想点击 "Allow" 或 "Deny" 按钮,但我无法使用以下代码执行此操作。
var Allow_Btn = driver.FindElementById("permission_allow_button");
Allow_Btn.Click();
经过大量谷歌搜索后,我发现我们可以使用以下功能跳过这些权限。
Capability.SetCapability("autoGrantPermissions", "true");
但是我想知道有没有其他方法可以做到。
提前致谢。
我不知道你从哪里得到这个 permission_allow_button
ID,我认为定位器不正确。
这是我在 Android Device Monitor application
中的样子因此,如果您仍想使用 strategy of finding the element by ID - 您应该使用 com.android.packageinstaller:id/permission_allow_button
ID。
XPath selectors 的另一个选项是:
//Button[@text()='ALLOW]
//Button[@resource-id='com.android.packageinstaller:id/permission_allow_button']
您只能通过 Xpath 定位器来完成。按 ID 查找不适用于权限对话框处理。 要查找 "ALLOW" 按钮,您可以使用
driver.find_element_by_xpath("//*[@class='android.widget.Button'][2]") //it denotes "Allow" button
为了"DENY"按钮你,
driver.find_element_by_xpath("//*[@class='android.widget.Button'][1]") //it denotes "Deny" button
在点击按钮之前,您应该等到找到元素
WebDriverWait wait = new WebDriverWait(driver, timeOut);
wait.until(ExpectedConditions.presenceOfElementLocated(element));
希望对您有所帮助。