Using the Browser Console for Accessibility Testing - A Practical Script Collection

Common browser console scripts for accessibility testing

At work, a few lines of JavaScript are all we need to highlight headings on any web page, check image alternative text, or inspect the distribution of landmarks.

The browser console is not just a developer debugging tool; it is also a sharp instrument for accessibility testing. By running simple scripts we can see the structure and accessibility features of a page directly, without changing the source code or installing complicated plugins.

What Is the Console?

The console is a tool built into modern browsers. It can:

  • Run temporary JavaScript code.

  • Quickly inspect page information.

  • Test ideas without changing the source code.

Code run in the console is valid only for the current session; refreshing the page restores everything.

How to Open the Console

  • On macOS:

    • Chrome: Option + Command + J

    • Firefox: Option + Command + K

    • Safari: first enable “Show Develop menu in menu bar” under “Settings → Advanced”, then press Option + Command + C.

  • On Windows:

    • Chrome / Edge / Firefox: Ctrl + Shift + J (or K)

Quick Start Examples

Before diving into complex scripts, try these two simple commands:

1. List all links on the page:

1
document.querySelectorAll("a");

2. Highlight all links on the page:

1
document.querySelectorAll("a").forEach(a => a.style.outline = "2px solid red");

14 Advanced Accessibility Testing Scripts

All the scripts below follow the same logic: create a transparent overlay at the top of the page, then add an outline and a floating badge to the corresponding elements.

How to Use:

  1. Open the browser console.

  2. Copy and paste the script below.

  3. Press ENTER to run it.

  4. Refresh the page to clear all highlights.

1. Show All Headings

This script marks headings with different colours by level (H1-H6).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
const overlay = document.createElement("div");
overlay.style.cssText = "position:absolute; top:0; left:0; width:100%; pointer-events:none; z-index:999999;";
document.body.appendChild(overlay);

const colours = { H1: "red", H2: "green", H3: "blue", H4: "purple", H5: "deepPink", H6: "black" };

document.querySelectorAll("h1, h2, h3, h4, h5, h6").forEach(h => {
    const tag = h.tagName.toUpperCase();
    const colour = colours[tag] || "#147580";
    h.style.outline = `3px solid ${colour}`;
    const rect = h.getBoundingClientRect();
    const badge = document.createElement("div");
    badge.textContent = tag;
    badge.style.cssText = `position:absolute; left:${rect.left + window.scrollX - 20}px; top:${rect.top + window.scrollY - 30}px; background:${colour}; color:white; padding:5px 8px; border-radius:5px; font-size:16px; z-index:999999;`;
    overlay.appendChild(badge);
});

2. Show All Buttons and Their Accessible Names

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const overlay = document.createElement("div");
overlay.style.cssText = "position:absolute; top:0; left:0; width:100%; pointer-events:none; z-index:999999;";
document.body.appendChild(overlay);

function getAccessibleName(el) {
    const ariaLabel = el.getAttribute("aria-label");
    if (ariaLabel && ariaLabel.trim()) return ariaLabel.trim();
    const labelledby = el.getAttribute("aria-labelledby");
    if (labelledby) {
        return labelledby.split(/\s+/).map(id => document.getElementById(id)?.textContent.trim()).filter(Boolean).join(" ");
    }
    if (el.tagName === "INPUT") return el.value.trim();
    return el.textContent.trim() || "(no name)";
}

const buttonSelector = 'button, [role="button"], input[type="button"], input[type="submit"], input[type="reset"]';
document.querySelectorAll(buttonSelector).forEach(btn => {
    const name = getAccessibleName(btn);
    btn.style.outline = "3px solid #ca6510";
    const rect = btn.getBoundingClientRect();
    const badge = document.createElement("div");
    badge.textContent = `Button: ${name}`;
    badge.style.cssText = `position:absolute; left:${rect.left + window.scrollX}px; top:${rect.top + window.scrollY - 28}px; background:#ca6510; color:white; padding:5px; border-radius:5px; font-size:16px; z-index:999999;`;
    overlay.appendChild(badge);
});

3. Check Image Alt Text

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
const overlay = document.createElement("div");
overlay.style.cssText = "position:absolute; top:0; left:0; width:100%; pointer-events:none; z-index:999999;";
document.body.appendChild(overlay);

document.querySelectorAll("img").forEach(img => {
    const alt = img.getAttribute("alt");
    const altText = (alt !== null) ? (alt.trim() || "(empty alt)") : "(missing alt)";
    img.style.outline = "2px solid #b23a48";
    const rect = img.getBoundingClientRect();
    const badge = document.createElement("div");
    badge.textContent = altText;
    badge.style.cssText = `position:absolute; left:${rect.left + window.scrollX}px; top:${rect.top + window.scrollY - 30}px; background:#b23a48; color:white; padding:5px; border-radius:5px; font-size:16px; z-index:999999;`;
    overlay.appendChild(badge);
});
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const overlay = document.createElement("div");
overlay.style.cssText = "position:absolute; top:0; left:0; width:100%; pointer-events:none; z-index:999999;";
document.body.appendChild(overlay);

function getLinkName(el) {
    const ariaLabel = el.getAttribute("aria-label");
    if (ariaLabel) return ariaLabel.trim();
    const text = el.textContent.trim();
    if (text) return text;
    const img = el.querySelector("img");
    if (img) return img.getAttribute("alt") || "(img no alt)";
    return "(no name)";
}

document.querySelectorAll("a").forEach(a => {
    const name = getLinkName(a);
    a.style.outline = "2px solid #6a1b9a";
    const rect = a.getBoundingClientRect();
    const badge = document.createElement("div");
    badge.textContent = name;
    badge.style.cssText = `position:absolute; left:${rect.left + window.scrollX}px; top:${rect.top + window.scrollY - 28}px; background:#6a1b9a; color:white; padding:4px 6px; border-radius:4px; font-size:16px; z-index:999999;`;
    overlay.appendChild(badge);
});

5. Show Lists and List Items

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
const overlay = document.createElement("div");
overlay.style.cssText = "position:absolute; top:0; left:0; width:100%; pointer-events:none; z-index:999999;";
document.body.appendChild(overlay);

function draw(el, label, color) {
    el.style.outline = `3px solid ${color}`;
    const rect = el.getBoundingClientRect();
    const badge = document.createElement("div");
    badge.textContent = label;
    badge.style.cssText = `position:absolute; left:${rect.left + window.scrollX}px; top:${rect.top + window.scrollY - 28}px; background:${color}; color:white; padding:5px; border-radius:5px; font-size:16px; z-index:999999;`;
    overlay.appendChild(badge);
}

document.querySelectorAll("ul").forEach(el => draw(el, "UL", "#006d77"));
document.querySelectorAll("ol").forEach(el => draw(el, "OL", "#005f8f"));
document.querySelectorAll("li").forEach(el => draw(el, "LI", "#8a5a44"));

6. Show Table Captions and Headers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
const overlay = document.createElement("div");
overlay.style.cssText = "position:absolute; top:0; left:0; width:100%; pointer-events:none; z-index:999999;";
document.body.appendChild(overlay);

document.querySelectorAll("table").forEach(table => {
    table.style.outline = "3px solid #d35400";
    const caption = table.querySelector("caption")?.textContent.trim() || "(no caption)";
    const rect = table.getBoundingClientRect();
    const badge = document.createElement("div");
    badge.textContent = `Table: ${caption}`;
    badge.style.cssText = `position:absolute; left:${rect.left + window.scrollX}px; top:${rect.top + window.scrollY - 30}px; background:#d35400; color:white; padding:4px 6px; border-radius:4px; font-size:14px; z-index:999999;`;
    overlay.appendChild(badge);
});

document.querySelectorAll("th").forEach(th => {
    th.style.outline = "2px dashed #8e44ad";
});

7. Show Landmark Roles

Landmark regions (such as header, nav, main) are essential for screen reader users.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const overlay = document.createElement("div");
overlay.style.cssText = "position:absolute; top:0; left:0; width:100%; pointer-events:none; z-index:999999;";
document.body.appendChild(overlay);

const roleColors = { banner: "#006d77", navigation: "#005f8f", main: "#4a148c", contentinfo: "#8a5a44", region: "#7f3d9c", complementary: "#b83b5e", search: "#d35400" };

function getRole(el) {
    const role = el.getAttribute("role");
    if (role) return role;
    const tag = el.tagName.toLowerCase();
    if (tag === "header") return "banner";
    if (tag === "nav") return "navigation";
    if (tag === "main") return "main";
    if (tag === "footer") return "contentinfo";
    if (tag === "aside") return "complementary";
    return null;
}

document.querySelectorAll('header, nav, main, footer, aside, section, article, [role]').forEach(el => {
    const role = getRole(el);
    if (!roleColors[role]) return;
    el.style.outline = `3px solid ${roleColors[role]}`;
    const rect = el.getBoundingClientRect();
    const badge = document.createElement("div");
    badge.textContent = role;
    badge.style.cssText = `position:absolute; left:${rect.left + window.scrollX}px; top:${rect.top + window.scrollY}px; background:${roleColors[role]}; color:white; padding:5px; border-radius:5px; font-size:16px; z-index:999999;`;
    overlay.appendChild(badge);
});

8. Show Form Label Names

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
const overlay = document.createElement("div");
overlay.style.cssText = "position:absolute; top:0; left:0; width:100%; pointer-events:none; z-index:999999;";
document.body.appendChild(overlay);

function getLabel(el) {
    if (el.id) {
        const label = document.querySelector(`label[for="${el.id}"]`);
        if (label) return label.textContent.trim();
    }
    return el.getAttribute("aria-label") || "(no label)";
}

document.querySelectorAll('input, select, textarea').forEach(el => {
    const name = getLabel(el);
    el.style.outline = "2px solid #0a558c";
    const rect = el.getBoundingClientRect();
    const badge = document.createElement("div");
    badge.textContent = name;
    badge.style.cssText = `position:absolute; left:${rect.left + window.scrollX}px; top:${rect.top + window.scrollY - 30}px; background:#0a558c; color:white; padding:5px; border-radius:5px; font-size:16px; z-index:999999;`;
    overlay.appendChild(badge);
});

9. Show Form Field Descriptions (aria-describedby)

1
2
3
4
5
6
document.querySelectorAll("[aria-describedby]").forEach(el => {
    const ids = el.getAttribute("aria-describedby").split(/\s+/);
    const description = ids.map(id => document.getElementById(id)?.textContent.trim()).filter(Boolean).join(" ");
    el.style.outline = "3px solid #0d6efd";
    console.log("Description for", el, ":", description);
});

10. Show Fieldsets and Legends

1
2
3
4
5
document.querySelectorAll("fieldset").forEach(fs => {
    const legend = fs.querySelector("legend")?.textContent.trim() || "(no legend)";
    fs.style.outline = "3px solid #1e8449";
    console.log("Fieldset:", legend);
});

11. Live Focus Tracking

After running this script, press TAB to move focus and the console will print the currently focused element in real time.

1
2
3
document.addEventListener('focusin', () => {
    console.log('Focused element:', document.activeElement);
});

12. Show aria-owns Relationships

1
2
3
4
document.querySelectorAll("[aria-owns]").forEach(el => {
    el.style.outline = "3px solid #c2185b";
    console.log("Element owns:", el.getAttribute("aria-owns"));
});

13. Show aria-controls Relationships

1
2
3
4
document.querySelectorAll("[aria-controls]").forEach(el => {
    el.style.outline = "3px solid #00796b";
    console.log("Element controls:", el.getAttribute("aria-controls"));
});

14. Focus Order Tracker

After running it, every time you press TAB a numbered badge appears on the page marking the order of your actions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
let focusStep = 0;
const badge = document.createElement("div");
badge.style.cssText = "position:absolute; background:#111; color:white; padding:5px 8px; border-radius:6px; font-size:16px; z-index:999999; pointer-events:none;";
document.body.appendChild(badge);

document.addEventListener('focusin', (e) => {
    const el = e.target;
    if (el === document.body) return;
    focusStep++;
    el.style.outline = "3px solid #111";
    const rect = el.getBoundingClientRect();
    badge.textContent = `${focusStep}. ${el.tagName.toLowerCase()}`;
    badge.style.left = `${rect.left + window.scrollX}px`;
    badge.style.top = `${rect.top + window.scrollY - 30}px`;
});

Summary

The advantage of testing with the console lies in immediacy and flexibility. You do not need to install any extension; a single script lets you look at your web page again from a machine’s point of view. I hope these scripts help you build a more inclusive web environment!

Built with Hugo
Theme Stack designed by Jimmy