karatelabs/karate · error · DriverException

no page at index

Error message

no page at index: ${index}

What it means

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.

Solutions

  1. Log driver.getPages() and switch by matching title/URL (switchPage / switchPageById) instead of a brittle index
  2. Wait for the new tab to appear (drainOpenedTargets or retry) before switching by index
  3. Clamp/validate the index against pages.size() in test code
  4. Fix off-by-one: the first tab is index 0

Example fix

// before
driver.switchPage(2); // assumes popup exists
// after
List<String> pages = driver.getPages();
if (pages.size() > 2) {
    driver.switchPage(2);
} else {
    driver.switchPage("expected-title-substring"); // or fail with a clear message
}
Defensive patterns

Strategy: validation

Validate before calling

List<String> pages = driver.getPages();
if (index < 0 || index >= pages.size()) {
    throw new IllegalArgumentException("index " + index + " out of range, have " + pages.size() + " pages");
}
driver.switchPage(index);

Try / catch

try {
    driver.switchPage(2);
} catch (DriverException e) {
    if (e.getMessage().startsWith("no page at index")) {
        logger.error("available pages={}", driver.getPages());
    }
    throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/driver/cdp/CdpDriver.java:4032

        }
        List<Map<String, Object>> drained = new ArrayList<>();
        Map<String, Object> entry;
        while ((entry = openedTargets.poll()) != null) {
            drained.add(entry);
        }
        return drained;
    }

    /**
     * Switch to a page by index.
     *
     * @param index the zero-based index
     */
    public void switchPage(int index) {
        logger.debug("switch page by index: {}", index);
        List<String> pages = getPages();
        if (index < 0 || index >= pages.size()) {
            throw new DriverException("no page at index: " + index);
        }
        activateTarget(pages.get(index));
    }

    // ========== Positional Locators ==========

    /**
     * Create a finder for elements to the right of the reference element.
     *
     * @param locator the reference element locator
     * @return a Finder for elements to the right
     */
    public Finder rightOf(String locator) {
        return new Finder(this, locator, Finder.Position.RIGHT_OF);
    }

    /**
     * Create a finder for elements to the left of the reference element.

View on GitHub (pinned to a22eb90246)