Run an axe Accessibility Scan in a Selenium Test
Scan a page with axe-core from inside an existing Selenium test, fail only on serious WCAG violations, and print readable results with selectors.
Problem
Your UI tests already reach every important page in the logged-in state you need. Accessibility is checked manually once a quarter, and regressions (an icon button with no label, a form field with no <label>, low-contrast text) ship in between. You want automated checks without a separate tool that cannot log in.
Solution
Run axe-core against the page the test is already on. Deque publishes Selenium integrations for Java, JavaScript and .NET; in Python, inject axe.min.js yourself and call axe.run() with an async script. Filter to WCAG A/AA rules and fail on serious and critical impact.
// Maven: com.deque.html.axe-core:seleniumimport com.deque.html.axecore.selenium.AxeBuilder;import com.deque.html.axecore.results.Results;import com.deque.html.axecore.results.Rule;
driver.get("https://app.example.com/checkout");
Results results = new AxeBuilder() .withTags(List.of("wcag2a", "wcag2aa", "wcag21a", "wcag21aa")) .analyze(driver);
List<Rule> serious = results.getViolations().stream() .filter(v -> "serious".equals(v.getImpact()) || "critical".equals(v.getImpact())) .toList();
String report = serious.stream() .map(v -> v.getId() + " (" + v.getImpact() + "): " + v.getHelp() + "\n " + v.getNodes().stream().map(n -> n.getTarget().toString()).collect(Collectors.joining("\n "))) .collect(Collectors.joining("\n"));assertTrue(serious.isEmpty(), "Accessibility violations:\n" + report);from pathlib import Path
AXE_JS = Path("node_modules/axe-core/axe.min.js").read_text(encoding="utf-8")
def run_axe(driver, tags=("wcag2a", "wcag2aa", "wcag21a", "wcag21aa")): driver.execute_script(AXE_JS) return driver.execute_async_script(""" const done = arguments[arguments.length - 1]; axe.run(document, { runOnly: { type: 'tag', values: arguments[0] } }) .then(done) .catch(err => done({ error: String(err) })); """, list(tags))
driver.get("https://app.example.com/checkout")results = run_axe(driver)assert "error" not in results, results.get("error")
serious = [v for v in results["violations"] if v["impact"] in ("serious", "critical")]report = "\n".join( f"{v['id']} ({v['impact']}): {v['help']}\n " + "\n ".join(str(n["target"]) for n in v["nodes"]) for v in serious)assert not serious, "Accessibility violations:\n" + report// npm i @axe-core/webdriverjsconst AxeBuilder = require('@axe-core/webdriverjs');
await driver.get('https://app.example.com/checkout');
const results = await new AxeBuilder(driver).withTags(['wcag2a', 'wcag2aa', 'wcag21a', 'wcag21aa']).analyze();
const serious = results.violations.filter((v) => ['serious', 'critical'].includes(v.impact));const report = serious.map((v) => `${v.id} (${v.impact}): ${v.help}\n ` + v.nodes.map((n) => n.target.join(' ')).join('\n ')).join('\n');assert.strictEqual(serious.length, 0, 'Accessibility violations:\n' + report);using Deque.AxeCore.Selenium;
driver.Navigate().GoToUrl("https://app.example.com/checkout");
var results = new AxeBuilder(driver) .WithTags("wcag2a", "wcag2aa", "wcag21a", "wcag21aa") .Analyze();
var serious = results.Violations .Where(v => v.Impact is "serious" or "critical") .ToList();
var report = string.Join("\n", serious.Select(v => $"{v.Id} ({v.Impact}): {v.Help}\n " + string.Join("\n ", v.Nodes.Select(n => n.Target.ToString()))));Assert.That(serious, Is.Empty, "Accessibility violations:\n" + report);Why It Works
axe-core is a JavaScript rules engine that inspects the live DOM, computed styles and accessibility tree from inside the page. Running it through Selenium means it sees exactly the state your test produced: logged in, modal open, form half-filled, error messages visible. The tag filter keeps the rule set to the WCAG level you are committed to, and the impact filter lets you start strict on the worst problems without drowning in minor ones.
Rolling It Out
| Stage | What to do |
|---|---|
| Baseline | Run on key pages, log all violations, fail nothing. Fix or ticket what you find. |
| Gate | Fail on critical and serious. Log moderate and minor to the report. |
| Expand | Scan after state changes: open menus, dialogs, validation errors. Many issues only exist in those states. |
Gotchas
- Automated scans catch roughly a third of real issues. Keyboard traps, focus order, meaningful alt text and screen reader flow still need human testing. A clean axe run is a floor, not a certificate.
- Scan after the page settles. Running during a loading state reports violations on skeletons and spinners. Wait for content first.
- Third-party widgets (chat, cookie banners) often fail. Exclude their containers with axe’s
excludecontext option rather than disabling rules globally. - Iframes. axe scans same-origin iframes automatically when it is injected into them; the Deque builders handle this. Cross-origin frames need their own scan.
- Pin the axe-core version. New rules are added in minor releases and can fail a previously clean build. Upgrade deliberately.
Learn the theory
- JavaScript Executor Execute JavaScript directly in the browser for advanced interactions and DOM manipulation.
- Reporting: Allure, ExtentReports and JUnit XML Turn raw test output into reports people read: JUnit XML for CI, Allure for rich history and attachments, ExtentReports for single-file HTML, with screenshots, logs and steps attached automatically.