{"record":{"id":"604dae7db0da5f1e","repo":"karatelabs/karate","slug":"no-page-found-with-targetid-targetid","errorCode":null,"errorMessage":"no page found with targetId: ${targetId}","messagePattern":"no page found with targetId: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":4003,"sourceCode":"        logger.debug(\"switch page by targetId: {}\", targetId);\n        if (targetId == null) {\n            throw new DriverException(\"targetId is null\");\n        }\n        // Verify the target exists as a page target before activating, retrying within\n        // the auto-wait budget: Target.getTargets can briefly lag a freshly opened tab.\n        int interval = fastPollInterval();\n        int maxAttempts = fastPollAttempts();\n        for (int attempt = 0; ; attempt++) {\n            if (getPages().contains(targetId)) {\n                activateTarget(targetId);\n                return;\n            }\n            if (attempt >= maxAttempts) {\n                break;\n            }\n            sleep(interval);\n        }\n        throw new DriverException(\"no page found with targetId: \" + targetId);\n    }\n\n    /**\n     * Drain the queue of page targets (tabs) opened since the last call.\n     * Event-driven — no CDP round-trip. See {@link Driver#drainOpenedTargets()}.\n     */\n    @Override\n    public List<Map<String, Object>> drainOpenedTargets() {\n        if (openedTargets.isEmpty()) {\n            return java.util.Collections.emptyList();\n        }\n        List<Map<String, Object>> drained = new ArrayList<>();\n        Map<String, Object> entry;\n        while ((entry = openedTargets.poll()) != null) {\n            drained.add(entry);\n        }\n        return drained;\n    }","sourceCodeStart":3985,"sourceCodeEnd":4021,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L3985-L4021","documentation":"CdpDriver.switchPageById(targetId) verifies, within its auto-wait budget, that a page target with the given targetId still exists before activating it; after exhausting fast-poll attempts it throws this DriverException. This guards against activating stale or already-closed targets.","triggerScenarios":"switchPageById(id) where the tab with that id was closed, navigated in a way that replaced the target, the id is from a previous browser session, or Target.getTargets keeps omitting it (e.g. it is an iframe/worker target, not a page).","commonSituations":"Test closed the tab earlier in the same scenario; reusing target ids captured before a driver restart; hard refresh or SPA reload replaced the target id; id points at a non-page target type.","solutions":["Refresh the list of valid ids via getPages()/targets and pick an existing id before switching","Re-capture the target id after any reload or driver restart instead of caching it","Increase the fast-poll budget in config if the tab is opened just-in-time","Verify the id refers to a page-type target (not iframe/worker) via devtools Target.getTargets"],"exampleFix":"// before\nString savedId = capturedTargetId; // from earlier scenario step\ndriver.switchPageById(savedId);\n// after\nString freshId = driver.getPages().stream().filter(id -> pageMatches(id)).findFirst().orElse(null);\ndriver.switchPageById(freshId);","handlingStrategy":"fallback","validationCode":"String freshId = driver.getPages().stream()\n    .filter(id -> id.equals(savedId))\n    .findFirst().orElse(null);\nif (freshId == null) { logger.warn(\"saved targetId no longer exists; re-resolving\"); }","typeGuard":null,"tryCatchPattern":"try {\n    driver.switchPageById(savedId);\n} catch (DriverException e) {\n    if (e.getMessage().contains(\"no page found with targetId\")) {\n        driver.switchPage(expectedUrlSubstring); // fallback: re-resolve by URL\n        return;\n    }\n    throw e;\n}","preventionTips":["Re-capture target ids after reloads or driver restarts; never cache across sessions","Prefer switching by URL/title substring over long-lived target ids","Check that the id is a page target, not an iframe/worker, before switching"],"tags":["driver","page-switch","target-not-found","stale-target"],"backgroundTag":"resource-not-found","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}