{"record":{"id":"ef4ab3a5eb9d088b","repo":"karatelabs/karate","slug":"dialog-accept-failed-error","errorCode":null,"errorMessage":"dialog accept failed: {error}","messagePattern":"dialog accept failed: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDialog.java","lineNumber":94,"sourceCode":"    @Override\n    public void accept(String promptText) {\n        if (handled) {\n            // Already handled - silently return to avoid race condition errors\n            // This can happen when multiple dialog events fire or auto-dismiss races with handler\n            return;\n        }\n        handled = true;\n        CdpMessage message = cdp.method(\"Page.handleJavaScriptDialog\")\n                .param(\"accept\", true);\n        if (promptText != null) {\n            message.param(\"promptText\", promptText);\n        }\n        CdpResponse response = message.send();\n        // If CDP call failed (e.g., dialog already gone), still consider it handled\n        // to prevent retry attempts that will also fail\n        if (response.isError()) {\n            // Log but don't throw - the dialog is effectively handled (gone)\n            throw new DriverException(\"dialog accept failed: \" + response.getError());\n        }\n    }\n\n    @Override\n    public void dismiss() {\n        if (handled) {\n            // Already handled - silently return to avoid race condition errors\n            return;\n        }\n        handled = true;\n        CdpResponse response = cdp.method(\"Page.handleJavaScriptDialog\")\n                .param(\"accept\", false)\n                .send();\n        if (response.isError()) {\n            throw new DriverException(\"dialog dismiss failed: \" + response.getError());\n        }\n    }\n","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDialog.java#L76-L112","documentation":"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.","triggerScenarios":"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.","commonSituations":"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().","solutions":["Guard the accept with isHandled() before calling, and skip if already handled","Avoid double-handling: either configure automatic dialog handling OR manual accept/dismiss, not both","Catch DriverException around accept() and treat 'already gone' as success","Re-check the page state — if dialogs vanish unexpectedly, investigate navigation or script timing"],"exampleFix":"// before\nif (dialog != null) { dialog.accept(false); } // may race\n// after\nif (dialog != null && !dialog.isHandled()) {\n    try { dialog.accept(false); }\n    catch (Exception e) { /* dialog already gone */ }\n}","handlingStrategy":"validation","validationCode":"// before accepting\nif (dialog == null || dialog.isHandled()) {\n    return; // dialog already gone — nothing to do\n}","typeGuard":"boolean canAccept(CdpDialog d) { return d != null && !d.isHandled(); }","tryCatchPattern":"try { dialog.accept(false); }\ncatch (DriverException e) {\n    if (e.getMessage().startsWith(\"dialog accept failed\")) {\n        logger.info(\"dialog already gone, treating as handled\");\n    } else throw e;\n}","preventionTips":["Check isHandled() before manual accept/dismiss","Do not mix automatic dialog handling with manual accept","Avoid duplicate dialog handling in chained steps","Treat 'dialog already gone' as success in retry logic"],"tags":["cdp","dialog","race-condition"],"backgroundTag":"invalid-state-transition","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}