axe-core error no elements found for context selector
Fixes the axe-core no-elements-for-context error by correcting the scope selector or fixing render timing. Use it when axe.run errors on an explicit context. Not for empty audit results on a valid context, which is a real clean scan.
axe-core error no elements found for context selector - how to fix it
TL;DR
Point the context selector at something that exists: the error means axe found zero elements for the context you passed, usually a typo in the selector or a context evaluated before the content rendered. Verify the selector in the live DOM, wait for render, then re-run. One line of why: axe refuses to audit nothing silently, so it errors instead of returning a fake clean result.
The error, verbatim
Error: No elements found for context: "#checkout-form"
at axe.run (node_modules/axe-core/axe.js)
hint: the selector matched zero elements
Fix it step by step
Step 1: Reproduce the selector miss
node -e "console.log('run the audit script')" && node scripts/audit.js | rg -i 'no elements' | head -3Expected: The no-elements error repeats.
Step 2: Test the selector against the live page
curl -s https://example.com/checkout | rg -o 'id="checkout-form"|id="checkout"' | head -3Expected: Shows whether the id exists in the served HTML or only appears after JS render.
Step 3: Fix the selector or the timing
rg -n 'checkout-form' scripts/audit.js tests/ | head -10Expected: Shows where the context selector is set; correct the typo or wait for render before running axe.
Step 4: Re-run the audit
node scripts/audit.js | tail -4Expected: Axe runs against the found context and returns real results.
Step 5: Re-run twice to rule out flakes
npx pa11y https://example.com/ | tail -2Expected: Two consecutive clean runs before calling it fixed; scan tools flake under load, so one green run is not proof.
When to use this skill
- The scan tool itself fails or crashes instead of reporting violations
- Your a11y CI step errors out before any rule results appear
- You run this tooling (pa11y, lighthouse, cypress-axe, axe-playwright) in automation
When NOT to use this skill
- The tool runs fine and reports real violations, use the rule-specific skills instead
- The failure is in your app code, not the scanner
Compatibility
axe-core 4.8+ (axe.run with context parameter). Same error via axe-playwright and cypress-axe context options. Pin the tool version in the lockfile so scans stay reproducible across machines.
Variant phrasings
axe no elements found for context
Same error, same fix.
axe.run context selector empty
Practitioner phrasing, usually a typo or timing issue.
same failure locally and in CI
Scan tool failures are environmental; a fix that works on a laptop must also be verified under CI conditions.
Why it happens
Axe's context parameter scopes the audit to part of the page, and a selector matching nothing is almost always a bug, so axe errors loudly instead of auditing zero elements and reporting a misleading clean result. The usual causes are a typo in the selector, the selector targeting content that renders after axe runs (SPA timing), or the selector being evaluated in the wrong frame. The error is axe protecting you from a false negative. Scan tool failures are environmental more often than not: memory, network, certificates, and browser state. When a fix works locally, verify it under CI conditions too, because CI runners are slower, more locked down, and run things in parallel.
Edge cases
- Scope to the smallest context that contains what you are testing, but verify it matches first.
- For SPA content, wait for the render (waitForSelector) before calling axe.run with a context.
- A context of document (the default) never triggers this, it only fires on explicit selectors.
- Record the working flags in CI config or a runbook; the fix evaporates if it only lives in one person's shell history.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_3SR0HueHaxQ8rrVvAA4PVQ
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.