5

How do I handle alerts in an Android web application using Appium server (1.0.1) and the Android SDK?

The below code is not working on android:

driver.switchTo().accept().alert();

Error message:

> -modal window does not get closed
2
  • Can you post more detail of the error? Commented Jul 10, 2015 at 14:53
  • SwitchTo() doesn't work for Appium.
    – Deepak
    Commented Nov 20, 2019 at 9:43

9 Answers 9

4

You need to get the Alert before you try and accept it

This is code from some of the Appium Java Client Tests:

wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();

This should work most of the time.

If accept() isn't working, replace the driver.switchTo().alert(); and alert.accept(); with code to find the button and then click it.

If it's not finding the button wrap findElementBy(Method) code in a try/retry block, and then click on it.

2
  • this code will only make driver switch to alert but it won't accept it Commented Jul 11, 2014 at 9:15
  • Updated my answer with some more workarounds/solutions you could try. If neither of those work, please paste your Appium server logs
    – Jess
    Commented Jul 11, 2014 at 13:19
4

The best way is to use the appium inspector. Click on the element and copy the resource-id from it. Use this resource id in findElement(By.id()) method.

For me resource-id: android:id/button1

((AndroidDriver) driver).findElement(By.id("android:id/button1")).click();

This is for Android. For regular use you can use

driver.findElement(By.id("android:id/button1")).click();
1
  • 1
    Appium doesn't seem to recognize the system alerts like normal elements. Did your code work?
    – Emjey
    Commented May 3, 2017 at 13:23
1

Some alerts may be native Android's alerts, not generated by a browser. In this case the following code:

Alert alert = driver.switchTo().alert(); alert.accept();

may throw: WebDriverException: unknown error: unhandled inspector error: {"code":-32603,"message":"No JavaScript dialog to handle"}

To handle such alert, just switch to the native application context, make required actions, and then switch back to the browser:

AppiumDriver<WebElement> appiumDriver = (AppiumDriver<WebElement>) webDriver;
String currentContext = appiumDriver.getContext();
appiumDriver.context("NATIVE_APP");

// actions within the alert
appiumDriver.findElements(By.xpath(OK_BUTTON_LOCATOR)).click(); // put locator instead of OK_BUTTON_LOCATOR
appiumDriver.context(currentContext);

// continue working
1
WebElement btn = driver.findElement(By.xpath("//android.widget.Button[@content-desc='OK']"));
TouchAction act = new TouchAction(driver);
act.tap(241,320).perform();

(241,320) these are X & Y c ordinates of alert This work perfectly for me

0

Appium comes with a default capability to accept, dismiss alerts

capabilities.SetCapability("autoAcceptAlerts", true);
capabilities.SetCapability("autoDismissAlerts", true);
2
  • Can you tell me the syntax in ruby to set it to true?
    – Emjey
    Commented May 3, 2017 at 13:24
  • Be advised - Utilizing these capabilities will result in Appium auto-accepting or dismissing the alerts.
    – Nikola
    Commented Jul 24, 2019 at 13:54
0

If the alert display on Ui , taking more time to display when we need to wait ..we can use fluent wait instead of this..

0

So an updated answer on this is this: an AlertDialog is a system level element, so clicking on accept button you should use:

androidDriver.findElementById("android:id/button1").click()

else for cancel do this:

androidDriver.findElementById("android:id/button2").click()
0

// first check alert is present or not

public boolean isAlertPresent() {
    try {
        mobDriver.switchTo().alert();
        log.info("ALERT IS PRESENT !! ");
        return true;
    } catch (Exception e) {
        log.info("ALERT IS NOT PRESENT !! ");
        return false;
    }
}

public void mobileAlertHandle() {
    if (isAlertPresent()) {
        Alert alert = mobDriver.switchTo().alert();
        alert.accept();
   }
}

if this does not work then inspect your element and try with id or name

ex: mobDriver.findElementById("android:id/button2").click()

-1

Please use the below code, Add some wait before clicking on OK Button. After that pass the xpath of you OK Button.

synchronized (driver)
{
driver.wait(2000);
}
driver.context(NATIVE_APP);
driver.findElementByXPath{("//android.widget.Button[@resourceid=
‘android:id/button1’]").click();
1
  • Why is wait() required here? Don't just post a code, add an explanation why it works.
    – sjaustirni
    Commented Nov 16, 2017 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.