karatelabs/karate · error · DriverException

dialog accept failed

Error message

dialog accept failed: {error}

What it means

CdpDialog.accept() sends Page.handleJavaScriptDialog with accept=true. If the CDP response reports an error (usually because the dialog already disappeared), a DriverException 'dialog accept failed: <error>' is thrown. The accompanying comment notes the dialog is effectively gone, so the error mainly signals a race, not a stuck dialog.

Solutions

  1. Guard the accept with isHandled() before calling, and skip if already handled
  2. Avoid double-handling: either configure automatic dialog handling OR manual accept/dismiss, not both
  3. Catch DriverException around accept() and treat 'already gone' as success
  4. Re-check the page state — if dialogs vanish unexpectedly, investigate navigation or script timing

Example fix

// before
if (dialog != null) { dialog.accept(false); } // may race
// after
if (dialog != null && !dialog.isHandled()) {
    try { dialog.accept(false); }
    catch (Exception e) { /* dialog already gone */ }
}
Defensive patterns

Strategy: validation

Validate before calling

// before accepting
if (dialog == null || dialog.isHandled()) {
    return; // dialog already gone — nothing to do
}

Type guard

boolean canAccept(CdpDialog d) { return d != null && !d.isHandled(); }

Try / catch

try { dialog.accept(false); }
catch (DriverException e) {
    if (e.getMessage().startsWith("dialog accept failed")) {
        logger.info("dialog already gone, treating as handled");
    } else throw e;
}

Prevention

When it happens

Trigger: Calling accept() on a dialog that was already dismissed/closed (by user code, another handler, or page navigation); the dialog auto-closed before the CDP command ran; duplicate accept() calls on the same dialog object.

Common situations: Test automation clicking a link that triggers a dialog while a previous handler already accepted it; slow CDP round-trip letting the browser destroy the dialog first; tests that both configure auto-dialog handling and manually call accept().

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/ef4ab3a5eb9d088b. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDialog.java:94

    @Override
    public void accept(String promptText) {
        if (handled) {
            // Already handled - silently return to avoid race condition errors
            // This can happen when multiple dialog events fire or auto-dismiss races with handler
            return;
        }
        handled = true;
        CdpMessage message = cdp.method("Page.handleJavaScriptDialog")
                .param("accept", true);
        if (promptText != null) {
            message.param("promptText", promptText);
        }
        CdpResponse response = message.send();
        // If CDP call failed (e.g., dialog already gone), still consider it handled
        // to prevent retry attempts that will also fail
        if (response.isError()) {
            // Log but don't throw - the dialog is effectively handled (gone)
            throw new DriverException("dialog accept failed: " + response.getError());
        }
    }

    @Override
    public void dismiss() {
        if (handled) {
            // Already handled - silently return to avoid race condition errors
            return;
        }
        handled = true;
        CdpResponse response = cdp.method("Page.handleJavaScriptDialog")
                .param("accept", false)
                .send();
        if (response.isError()) {
            throw new DriverException("dialog dismiss failed: " + response.getError());
        }
    }

View on GitHub (pinned to a22eb90246)