axe-core code samples for the browser console
Copy and paste these samples into the browser console, in order, on the page you want to test. Samples 1 to 3 set things up. Samples 4 to 10 explore the results. Sample 11 combines loading and reporting into one snippet, and sample 12 downloads a full HTML report.
Clearing the console
If the console gets too long or crowded, you can clear it using:
- Keyboard: Ctrl + L (Windows/Linux) or Cmd + K (Mac)
- Command: type
clear()orconsole.clear()and press Enter - Button: click the "Clear console" icon at the top-left of the Console panel
Clearing the console only clears what's on screen. axe-core and your results are still there, so you can carry on with the next sample.
1. Load axe-core into the page
Adds a script element that pulls axe-core from a CDN. Wait a second or two for it to finish loading before moving on.
var script = document.createElement('script');
script.src =
'https://cdn.jsdelivr.net/npm/axe-core@4.14.0/axe.min.js';
document.head.appendChild(script);
Expected result: Nothing is printed, or the console shows the script element. If a page blocks external scripts (a Content Security Policy), the load will fail. Try a different page.
2. Check axe has loaded
Asks axe-core which version it is.
axe.version
Expected result: The console prints the version number, '4.14.0'. If axe has not loaded, it prints ReferenceError: axe is not defined. Wait a moment and try again.
3. Run axe
Tests the page and stores everything axe finds in a variable called results.
var results = await axe.run();
Expected result: The console prints undefined. That's normal. The results have been stored in results, ready for the next samples.
4. Count each list
The results object holds four lists. Check how many items are in each.
console.log('violations:', results.violations.length);
console.log('passes:', results.passes.length);
console.log('incomplete:', results.incomplete.length);
console.log('inapplicable:', results.inapplicable.length);
What each list holds
violations: rules that failedpasses: rules that passedincomplete: items axe could not decide, which need a manual checkinapplicable: rules with nothing on the page to test
Expected result: Four lines, each with a list name and a count.
5. Look at every failed rule
Prints every item in the violations list, each labelled with its impact and rule name.
results.violations.forEach(v => console.log(v.impact, v.id, v));
Expected result: One line per failed rule, starting with its impact and rule name, such as critical button-name, followed by the full result. Expand any line to see its details.
6. Show the violations as a table
One row per failed rule, with the rule name, how severe it is and how many elements failed.
console.table(
results.violations.map(v => ({
rule: v.id,
impact: v.impact,
elements: v.nodes.length
}))
);
Expected result: The console prints a table with the columns rule, impact and elements.
7. Filter to critical and serious
Keeps only the most severe failures, then shows them as a table.
var important = results.violations.filter(v =>
v.impact === 'critical' || v.impact === 'serious'
);
console.table(
important.map(v => ({
rule: v.id,
impact: v.impact,
elements: v.nodes.length
}))
);
Expected result: The table only lists rules with an impact of critical or serious. The other levels are moderate and minor.
8. See which elements failed
Lists each failed rule, then a selector for every element that failed it.
results.violations.forEach(v => {
console.log(v.id);
v.nodes.forEach(n => {
console.log(' ', n.target.join(' '));
});
});
Expected result: Each rule name is followed by an indented list of CSS selectors, one per failing element.
9. Highlight the failing elements
Draws a red outline around every element that failed.
results.violations.forEach(v => {
v.nodes.forEach(n => {
var el = document.querySelector(n.target[0]);
if (el) {
el.style.outline = '4px solid red';
}
});
});
Expected result: Failing elements get a red outline on the page. Refresh the page to remove the outlines. This simple version may miss elements inside iframes or shadow DOM.
10. Show fixes
Prints each failed rule with its plain-language help text, a link to the full rule description, and what to change on each failing element.
results.violations.forEach(v => {
console.log(v.help);
console.log(v.helpUrl);
v.nodes.forEach(n => {
console.log(n.target.join(' '));
console.log(n.failureSummary);
});
});
Expected result: Each failing element is followed by text starting with Fix any of the following: or Fix all of the following:, then the specific changes needed.
11. Load axe and show the table in one snippet
Everything needed in a single copy and paste: load axe-core if it isn't already on the page, run it, and show the results. It's safe to paste more than once.
(async () => {
if (!window.axe) {
const script = document.createElement('script');
script.src = 'https://cdn.jsdelivr.net/npm/axe-core@4.14.0/axe.min.js';
const loaded = new Promise((resolve, reject) => {
script.onload = resolve;
script.onerror = () => reject(new Error('axe-core failed to load (check CSP or version)'));
});
document.head.appendChild(script);
await loaded;
}
const results = await axe.run();
window.results = results;
console.log(`${results.violations.length} violations`);
console.table(results.violations.map(v => ({ rule: v.id, impact: v.impact, elements: v.nodes.length })));
console.log(results);
})();
Expected result: After a short pause, the console prints the number of violations, a table of the failed rules, then the full results object. If axe-core can't load, you'll see the error axe-core failed to load (check CSP or version). The results variable is still available, so samples 4 to 10 will also work.
Reusing the script
Save sample 11 as a Snippet in Chrome, so you don't need to paste it each time:
- In DevTools, open the Sources tab.
- In the left panel, select Snippets (click » if you can't see it).
- Click + New snippet, and name it (for example,
axe-scan). - Paste the script and save with Ctrl + S (Windows) or Cmd + S (Mac).
- To run it, right-click the snippet and choose Run, or press Ctrl + Enter (Windows) or Cmd + Enter (Mac).
Snippets are saved in your browser, so they're still there next time you open DevTools, on any page.
12. Download a full HTML report
Everything in one copy and paste: load axe-core if it isn't already on the page, run it, then download the results as an HTML file called axe-report.html. It's safe to paste more than once.
This snippet:
- Loads axe-core if it isn't already on the page
- Runs axe on the page
- Makes text safe to put inside HTML
- Builds one section for fails and one for needs review
- Puts the whole report page together
- Downloads it as a file
(async () => {
// 0. Load axe-core if it isn't already on the page
if (!window.axe) {
await new Promise((resolve, reject) => {
const s = document.createElement('script');
s.src = 'https://cdn.jsdelivr.net/npm/axe-core@4.14.0/axe.min.js';
s.onload = resolve;
s.onerror = () => reject(new Error('Could not load axe-core. The site may block outside scripts.'));
document.head.appendChild(s);
});
}
// 1. Run axe on the page
const r = await axe.run();
// 2. Make text safe to put inside HTML
const esc = (s) => String(s ?? '').replace(/[&<>"]/g, (c) =>
({ '&': '&', '<': '<', '>': '>', '"': '"' }[c]));
// 3. Build one section (fails or needs review)
const section = (title, rules) => {
const count = rules.reduce((n, rule) => n + rule.nodes.length, 0);
let out = `<h2>${title} (${count})</h2>`;
if (!rules.length) return out + '<p>None.</p>';
for (const rule of rules) {
out += `<h3>${esc(rule.id)}: ${esc(rule.help)} (${rule.nodes.length})</h3>
<p>Impact: ${esc(rule.impact)}. <a href="${esc(rule.helpUrl)}">Rule details on Deque University</a></p>
<table><thead><tr><th scope="col">#</th><th scope="col">Element</th><th scope="col">Selector</th><th scope="col">Problem</th></tr></thead><tbody>`;
rule.nodes.forEach((n, i) => {
const msgs = [...n.any, ...n.all, ...n.none].map((c) => esc(c.message)).join('<br>');
out += `<tr><td>${i + 1}</td><td><code>${esc(n.html)}</code></td><td><code>${esc(n.target.join(' '))}</code></td><td>${msgs}</td></tr>`;
});
out += '</tbody></table>';
}
return out;
};
// 4. Put the whole page together
const html = `<!DOCTYPE html><html lang="en"><head><meta charset="utf-8">
<title>axe report: ${esc(document.title)}</title>
<style>
body { font-family: system-ui, sans-serif; line-height: 1.5; margin: 2rem; color: #1d1d1f; }
table { border-collapse: collapse; width: 100%; margin-bottom: 2rem; }
th, td { border: 1px solid #ccc; padding: .4rem .6rem; text-align: left; vertical-align: top; }
th { background: #f2f2f2; }
code { font-size: .85rem; word-break: break-word; }
h2 { border-top: 3px solid #1d1d1f; padding-top: 1rem; margin-top: 2.5rem; }
</style></head><body>
<h1>axe report</h1>
<ul>
<li>Page: <a href="${esc(r.url)}">${esc(document.title)}</a></li>
<li>Run: ${new Date(r.timestamp).toLocaleString()}</li>
<li>axe-core ${esc(r.testEngine.version)}</li>
<li>Rules passed: ${r.passes.length}. Rules not applicable: ${r.inapplicable.length}.</li>
</ul>
${section('Fails', r.violations)}
${section('Needs review', r.incomplete)}
</body></html>`;
// 5. Download it as a file
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([html], { type: 'text/html' }));
a.download = 'axe-report.html';
a.click();
})();
Expected result: The console prints a Promise, then the browser downloads axe-report.html. The report lists every failed and needs review rule, with a table of the elements and the exact problem for each. If axe-core can't load, you'll see the error Could not load axe-core. The site may block outside scripts.