skip to content

html, css, and dom · lesson 8 of 8 · sources checked 2026-11-20

extract from the accessibility tree

This lesson extracts the same fields from the DOM and the accessibility tree of a lab page, and shows which hidden text the tree drops.

status of this lesson: from a public source

purpose

A browser makes two views of one page: the DOM (Document Object Model) and the accessibility tree. A DOM query also finds text that no user sees, and the accessibility tree drops some of that text. Thus a collector can extract the same fields from each view, and the difference shows hidden text.

objectives

  1. Extract the same fields from the DOM and from the accessibility tree of a lab page.
  2. Name the hidden content that the accessibility tree drops, and the hidden content that it keeps.
  3. Compare the fields that each view finds, with a table of fields and dropped nodes.
  4. Explain why a site must test its traps against both views.

new terms

browser agent
Software in which a model decides the next action and code applies it to a real browser.

how it works

A DOM query reads each element of the page, also an element that no user sees. The accessibility tree reads the page as assistive technology reads it. Thus the two views can give different data from one page.

what the tree holds

WAI-ARIA 1.2 defines the accessibility tree as a "Tree of accessible objects that represents the structure of the user interface" W3C, source 1. Each node of the tree has a role, a name, and states. For example, a list item has the role listitem, and a text node has the text as its name.

The same standard gives the rules for hidden content:

  • An element with aria-hidden="true" is hidden from the accessibility API.
  • An element that is not rendered is hidden. An example is an element with display: none.
  • An element is hidden when one of its ancestors is hidden.

The DOM Standard defines the tree that a DOM query reads. For example, querySelector returns the first descendant element that matches the selectors WHATWG, source 2. The query does not ask if a user can see the element.

the two extractions

The lab page has a list of products, with a name, a price, and a stock field for each. It also has hidden text:

  • A decoy price: a false price with display: none.
  • A price with aria-hidden="true".
  • A price that CSS moves far off the screen.

A DOM query with the class of the price finds each of these prices. An extraction from the tree finds the visible prices and the price off the screen. It does not find the two other hidden prices, because the tree drops them.

For example, the code example finds 6 prices in the DOM view and 4 in the tree view.

what the tree does not filter

The tree is not a full filter against hidden text. Text that CSS hides in a different way, for example by a position off the screen, can stay in the tree. Also, a site can mark real content wrongly, for example with aria-hidden on a visible price. Then the tree drops a real field.

Thus compare the two views. A field that only the DOM finds is hidden text or a field that the site marks wrongly. Examine it before you store it.

agents that read the tree

A browser agent can read the tree in place of the DOM. Then it sees the same view as the extraction from the tree. It does not see a decoy element that the tree drops, so it does not select that decoy.

Thus a site that tests its traps only against a DOM client does not know the result for a client that reads the tree. For example, a DOM query finds a decoy with display: none, but a client that reads the tree never sees it.

the two sides

automation sidedetection side
The accessibility tree drops some hidden elements, so it is a cheap filter against hidden text. It also drops content that a site marks wrongly.An agent that reads the accessibility tree does not select a decoy element that the tree drops. The site must test its traps against both views.

design decisions

optiongaincostwhen it is correct
Extract from the DOM onlyEach field of the markup is available, also with no browser.Hidden decoy text enters the data.Pages with no hidden text, after a check
Extract from the accessibility tree onlyThe tree drops text with display: none, hidden, or aria-hidden.It keeps other hidden text, and it drops content that the site marks wrongly. It must have a browser.Pages where the visible text is the data
Extract from both views and compareA difference shows hidden text or a wrong mark.Two extractions for each page.New sites, and each change of a site

diagram

A matrix with two columns, the DOM and the accessibility tree. The DOM holds each kind of content. The tree holds the visible fields and the text off the screen. It drops text with display none, the hidden attribute, or aria-hidden. It can drop content that the site marks wrongly.

two views of one page: what the dom and the accessibility tree hold
content of the pagedomaccessibility tree
Visible name, price, and stock fieldsHolds themHolds them as roles and names
Text with display: noneHolds itDrops it
Text with the hidden attributeHolds itDrops it
Text with aria-hidden="true"Holds itDrops it
Text that CSS moves off the screenHolds itHolds it
Content that the site marks wronglyHolds itCan drop it
Only text that a rule of the standard hides goes out of the tree.source: My model of the two views of a page

code example

In a lab with a browser, a browser automation library gives the accessibility tree of the page. This example uses a small model of that tree, so it runs with no browser. The model drops an element that is not rendered or that has aria-hidden="true", with its subtree.

example The page is synthetic, and the tree is a model, not the output of a browser.

m05-x11-dom-vs-tree.tstypescript

Extract the prices of a synthetic page from the DOM and from a model of the accessibility tree. Show which prices only the DOM finds.

import { readFileSync } from "node:fs";

// Synthetic page only. A small model of the tree: no browser, no network.
type El = {
  tag: string; cls?: string; text?: string;
  display?: string; left?: number; hidden?: boolean; ariaHidden?: boolean; children?: El[];
};
type TreeNode = { role: string; name: string };

// DOM view: each element with the class, also an element that a user cannot see.
function domFields(el: El, cls: string, out: string[] = []): string[] {
  if (el.cls === cls && el.text) out.push(el.text);
  for (const c of el.children ?? []) domFields(c, cls, out);
  return out;
}

function countElements(el: El): number {
  return 1 + (el.children ?? []).reduce((n, c) => n + countElements(c), 0);
}

// Tree view: an element that is not rendered, or that has aria-hidden="true", is out with its subtree.
// Other CSS, for example a position far off the screen, does not remove it.
function treeView(el: El, out: TreeNode[] = []): TreeNode[] {
  if (el.display === "none" || el.hidden || el.ariaHidden) return out;
  if (el.text) out.push({ role: "text", name: el.text });
  else out.push({ role: el.tag === "li" ? "listitem" : "generic", name: "" });
  for (const c of el.children ?? []) treeView(c, out);
  return out;
}

const page: El = JSON.parse(readFileSync(process.argv[2], "utf8"));
const domPrices = domFields(page, "price");
const tree = treeView(page);
const treePrices = tree.filter((n) => n.name.startsWith("Price:")).map((n) => n.name);

console.log(`DOM elements: ${countElements(page)}, tree nodes: ${tree.length}`);
console.log(`prices in the DOM view: ${domPrices.length}`);
console.log(`prices in the tree view: ${treePrices.length}`);
for (const p of domPrices) {
  console.log(`  ${p}: ${treePrices.includes(p) ? "in both views" : "DOM only"}`);
}

output

The count of DOM elements and tree nodes, the count of prices in each view, and one line for each price.

DOM elements: 13, tree nodes: 11
prices in the DOM view: 6
prices in the tree view: 4
  Price: 40: in both views
  Price: 55: in both views
  Price: 1: DOM only
  Price: 120: in both views
  Price: 2: DOM only
  Price: 3: in both views

I ran the code with the Node runtime and the flag --experimental-strip-types, on a synthetic list of three products. The list also has a price with display: none, a price with aria-hidden, and a price far off the screen. Only the DOM view finds "Price: 1" and "Price: 2". The price off the screen, "Price: 3", is in both views.

data

fields and hidden text that each view finds on a lab page

status of this figure: measured by me

fields and hidden text that each view finds on a lab pagename fields (6)1: 62: 63: 6price fields (6)1: 62: 63: 6stock fields (6)1: 62: 63: 6hidden decoy prices (2)1: 22: 03: 0aria-hidden prices (2)1: 22: 03: 0hidden text parts (11)1: 92: 53: 5unit: items1. DOM query2. accessibility tree3. text form of the tree

source: Lab result of this course: DOM and accessibility tree on 3 lab-built pages; period 2026-11-16 to 2026-11-17; condition: Three lab-built pages on one host, one read of each page, a browser in new headless mode.

data table
fields and hidden text that each view finds on a lab page
seriesitem on the pageitems
DOM queryname fields (6)6
DOM queryprice fields (6)6
DOM querystock fields (6)6
DOM queryhidden decoy prices (2)2
DOM queryaria-hidden prices (2)2
DOM queryhidden text parts (11)9
accessibility treename fields (6)6
accessibility treeprice fields (6)6
accessibility treestock fields (6)6
accessibility treehidden decoy prices (2)0
accessibility treearia-hidden prices (2)0
accessibility treehidden text parts (11)5
text form of the treename fields (6)6
text form of the treeprice fields (6)6
text form of the treestock fields (6)6
text form of the treehidden decoy prices (2)0
text form of the treearia-hidden prices (2)0
text form of the treehidden text parts (11)5
Each value was the same on each of the 3 lab pages. The tree drops text that display:none, the hidden attribute, or aria-hidden hides, but not other text that CSS hides. The node counts depend on the count rule and are in the text.

On each lab page, the three views find each visible field. Only the DOM query finds the hidden decoy prices and the prices with aria-hidden. The third series is the text form of the tree that a browser automation library gives.

The node counts depend on the count rule. With the text leaves, the tree has more nodes than the DOM has elements. Without them, it has fewer:

lab pagedom elementstree nodes, not ignoredtree nodes, not ignored, without text leaves
Lab page 17415743
Lab page 29421763
Lab page 36412733

Thus do not compare the size of the two views by a count of nodes. Compare the fields that each view finds.

failure modes

problemcausecorrection
Hidden decoy text enters the data.The extraction reads the DOM, and a DOM query finds hidden elements.Extract from the tree too, and examine each field that only the DOM finds.
A real field is missing from the extraction from the tree.The site marks the field with aria-hidden, or hides it by a rule of the standard.Compare with the DOM view. Store the field only after a check of the page.
Text that no user sees is still in the extraction from the tree.CSS hides the text in a way that the tree does not filter, for example a position off the screen.Do not use the tree as the only filter. Add a visibility check with the computed style.
A trap of the site catches no agent.The site tests the trap only against a DOM client, and the agent reads the tree.Test each trap against both views.
Two reports of the tree size do not agree.They use different count rules, with or without text leaves.State the count rule with each count, or compare fields, not nodes.

check questions

  1. A page has a price with display: none. Which extraction finds this price?

    1. The extraction from the accessibility tree
    2. The DOM query
    3. Both extractions
    4. No extraction
    answer

    b. The DOM query

    A DOM query reads each element, also an element that is not rendered. The accessibility tree drops an element that is not rendered.

  2. Name the hidden content that the accessibility tree drops, and the hidden content that it can keep.

    sample answer

    The tree drops an element with aria-hidden="true" and an element that is not rendered, for example with display: none. It also drops each element whose ancestor is hidden. It can keep text that CSS hides in a different way, for example a position far off the screen.

  3. A field is in the DOM extraction and not in the extraction from the tree. What does this difference show?

    1. The field is hidden text, or the site marks a real field wrongly.
    2. The field is always a real visible field.
    3. The tree has fewer nodes, so it lost the field by chance.
    answer

    a. The field is hidden text, or the site marks a real field wrongly.

    The tree drops hidden content and content that the site marks wrongly. Examine the field before you store it.

  4. Explain why a site must test its traps against both views.

    sample answer

    A DOM query finds a trap such as a decoy element with display: none. An agent that reads the accessibility tree does not see that decoy, so it does not select it. A site that tests only against a DOM client does not know the result for a client that reads the tree.

exercise

exercise Do this task in your own lab, with synthetic data only. Do not send a request to a site.

  1. Save the code example in an empty folder of your lab.
  2. Write a JSON file with a list of two products, each with a name and a price.
  3. Give each price the class price and a text that starts with "Price:".
  4. Add a price with the field hidden set to true.
  5. Add a price with the field display set to "none" on its list item, not on the price.
  6. Run the code with Node, the flag --experimental-strip-types, and the file.
  7. Record which prices only the DOM view finds.
sample answer

This sample answer uses the synthetic file of the steps only.

The DOM view finds four prices. The tree view finds only the two visible prices. The price with hidden is out, because the model drops an element with hidden. The price inside the list item with display: none is also out, because an element is hidden when one of its ancestors is hidden. Thus the two hidden prices are "DOM only".

sources

  1. WAI-ARIA 1.2, W3C, standard, read 2026-11-20
  2. DOM Standard, WHATWG, standard, read 2026-11-20
  3. My model of the two views of a page and of the comparison of two extractions, 2026-11 (not public).
  4. Lab result of this course: the node counts of the DOM and the accessibility tree on 3 lab-built pages, 2026-11 (not public).