{"record":{"id":"01f3c517e67e27a3","repo":"karatelabs/karate","slug":"element-vanished-during-action-after-element-action-attempts","errorCode":null,"errorMessage":"element vanished during action after ${ELEMENT_ACTION_ATTEMPTS} re-resolve attempts: ${locator} | ${driverState}","messagePattern":"element vanished during action after (.+?) re-resolve attempts: (.+?) \\| (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":3268,"sourceCode":"     * </p>\n     */\n    private Object elementAction(String locator, String js) {\n        RuntimeException lastError = null;\n        for (int i = 0; i < ELEMENT_ACTION_ATTEMPTS; i++) {\n            retryIfNeeded(locator);\n            try {\n                return script(js);\n            } catch (RuntimeException e) {\n                if (!isElementVanished(e)) {\n                    throw e;\n                }\n                lastError = e;\n                logger.warn(\"element vanished between existence check and action, re-resolving ({}/{}): {}\",\n                        i + 1, ELEMENT_ACTION_ATTEMPTS, locator);\n                sleep(options.getRetryInterval());\n            }\n        }\n        throw new DriverException(\"element vanished during action after \" + ELEMENT_ACTION_ATTEMPTS\n                + \" re-resolve attempts: \" + locator + \" | \" + getDriverState(), lastError);\n    }\n\n    private static boolean isElementVanished(RuntimeException e) {\n        return e.getMessage() != null && e.getMessage().contains(Locators.ELEMENT_NOT_FOUND);\n    }\n\n    // ========== Wait Methods ==========\n\n    /**\n     * Wait for an element to exist.\n     */\n    public Element waitFor(String locator) {\n        return waitFor(locator, options.getTimeoutDuration());\n    }\n\n    /**\n     * Wait for an element to exist with custom timeout.","sourceCodeStart":3250,"sourceCodeEnd":3286,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L3250-L3286","documentation":"During an element action, Karate checks existence then performs the action; if the element disappears in between, it re-resolves up to ELEMENT_ACTION_ATTEMPTS times with the configured retry interval between tries. If every re-resolve also fails, it throws with the attempt count, locator, driver state, and the last underlying error as cause.","triggerScenarios":"Element removed/re-rendered by the app (SPA re-render, animation, route change) between the exists() check and the action, persistently across all ELEMENT_ACTION_ATTEMPTS re-resolution rounds.","commonSituations":"React/Vue lists re-rendering while the test clicks; loading spinners replacing the target node; tests acting on elements during a transition; aggressive DOM cleanup (virtualized lists) unmounting off-screen nodes.","solutions":["Wait for app stability before acting: wait for the loading spinner to disappear or a stable container to exist.","Re-run the action with retry-until semantics so a fresh attempt starts after the re-render settles.","Target a stable ancestor/container rather than a node the framework replaces.","Inspect the cause (lastError) in the exception to see whether a navigation or JS error removed the element."],"exampleFix":"// before\ndriver.click('.list-item.active'); // re-rendered mid-action\n// after\ndriver.waitForEnabled('.list-container');\ndriver.click('.list-container .list-item.active');","handlingStrategy":"retry","validationCode":"// act only after the UI is stable (no spinners)\nretryUntil(() -> !driver.exists(\".spinner\"));","typeGuard":null,"tryCatchPattern":"try {\n    driver.click(locator);\n} catch (Exception e) {\n    if (e.getMessage().contains(\"vanished during action\")) {\n        // cause holds the last underlying failure\n        sleep(optionsRetryInterval);\n        driver.click(locator);\n    } else throw e;\n}","preventionTips":["Wait for loading indicators to disappear before actions.","Prefer stable container selectors over re-rendered leaf nodes.","Avoid assertions mid-animation or mid-route-change.","Inspect the exception cause to identify what removed the node."],"tags":["driver","element-not-found","race-condition","retry","spa"],"backgroundTag":"element-vanished-during-action","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"}