{"record":{"id":"e6d9ec6a2485ed60","repo":"karatelabs/karate","slug":"no-page-at-index-index","errorCode":null,"errorMessage":"no page at index: ${index}","messagePattern":"no page at index: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java","lineNumber":4032,"sourceCode":"        }\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    }\n\n    /**\n     * Switch to a page by index.\n     *\n     * @param index the zero-based index\n     */\n    public void switchPage(int index) {\n        logger.debug(\"switch page by index: {}\", index);\n        List<String> pages = getPages();\n        if (index < 0 || index >= pages.size()) {\n            throw new DriverException(\"no page at index: \" + index);\n        }\n        activateTarget(pages.get(index));\n    }\n\n    // ========== Positional Locators ==========\n\n    /**\n     * Create a finder for elements to the right of the reference element.\n     *\n     * @param locator the reference element locator\n     * @return a Finder for elements to the right\n     */\n    public Finder rightOf(String locator) {\n        return new Finder(this, locator, Finder.Position.RIGHT_OF);\n    }\n\n    /**\n     * Create a finder for elements to the left of the reference element.","sourceCodeStart":4014,"sourceCodeEnd":4050,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java#L4014-L4050","documentation":"CdpDriver.switchPage(index) fetches the current page list and, if the zero-based index is negative or beyond the last page, throws this DriverException. It is an bounds check: you asked to focus a tab position that does not exist right now.","triggerScenarios":"switchPage(2) when only 1–2 tabs are open; negative index; calling before the new tab finished opening so getPages() still returns the old list.","commonSituations":"Hardcoded index assuming a popup opened, but it did not; off-by-one (zero-based indexing mistaken for one-based); previous test steps closed tabs changing the order; race where the switch runs before window.open resolves.","solutions":["Log driver.getPages() and switch by matching title/URL (switchPage / switchPageById) instead of a brittle index","Wait for the new tab to appear (drainOpenedTargets or retry) before switching by index","Clamp/validate the index against pages.size() in test code","Fix off-by-one: the first tab is index 0"],"exampleFix":"// before\ndriver.switchPage(2); // assumes popup exists\n// after\nList<String> pages = driver.getPages();\nif (pages.size() > 2) {\n    driver.switchPage(2);\n} else {\n    driver.switchPage(\"expected-title-substring\"); // or fail with a clear message\n}","handlingStrategy":"validation","validationCode":"List<String> pages = driver.getPages();\nif (index < 0 || index >= pages.size()) {\n    throw new IllegalArgumentException(\"index \" + index + \" out of range, have \" + pages.size() + \" pages\");\n}\ndriver.switchPage(index);","typeGuard":null,"tryCatchPattern":"try {\n    driver.switchPage(2);\n} catch (DriverException e) {\n    if (e.getMessage().startsWith(\"no page at index\")) {\n        logger.error(\"available pages={}\", driver.getPages());\n    }\n    throw e;\n}","preventionTips":["Remember indexes are zero-based","Validate against getPages().size() before switching by index","Prefer title/URL-based switching to avoid order-dependent tabs","Wait for the new tab (drainOpenedTargets) before indexing into it"],"tags":["driver","page-switch","index-out-of-bounds","tab"],"backgroundTag":"index-out-of-range","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"}