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 sourcepurpose
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
- Extract the same fields from the DOM and from the accessibility tree of a lab page.
- Name the hidden content that the accessibility tree drops, and the hidden content that it keeps.
- Compare the fields that each view finds, with a table of fields and dropped nodes.
- 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 side | detection 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
| option | gain | cost | when it is correct |
|---|---|---|---|
| Extract from the DOM only | Each 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 only | The 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 compare | A 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.
| content of the page | dom | accessibility tree |
|---|---|---|
| Visible name, price, and stock fields | Holds them | Holds them as roles and names |
| Text with display: none | Holds it | Drops it |
| Text with the hidden attribute | Holds it | Drops it |
| Text with aria-hidden="true" | Holds it | Drops it |
| Text that CSS moves off the screen | Holds it | Holds it |
| Content that the site marks wrongly | Holds it | Can drop it |
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.
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 viewsI 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
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
| series | item on the page | items |
|---|---|---|
| DOM query | name fields (6) | 6 |
| DOM query | price fields (6) | 6 |
| DOM query | stock fields (6) | 6 |
| DOM query | hidden decoy prices (2) | 2 |
| DOM query | aria-hidden prices (2) | 2 |
| DOM query | hidden text parts (11) | 9 |
| accessibility tree | name fields (6) | 6 |
| accessibility tree | price fields (6) | 6 |
| accessibility tree | stock fields (6) | 6 |
| accessibility tree | hidden decoy prices (2) | 0 |
| accessibility tree | aria-hidden prices (2) | 0 |
| accessibility tree | hidden text parts (11) | 5 |
| text form of the tree | name fields (6) | 6 |
| text form of the tree | price fields (6) | 6 |
| text form of the tree | stock fields (6) | 6 |
| text form of the tree | hidden decoy prices (2) | 0 |
| text form of the tree | aria-hidden prices (2) | 0 |
| text form of the tree | hidden text parts (11) | 5 |
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 page | dom elements | tree nodes, not ignored | tree nodes, not ignored, without text leaves |
|---|---|---|---|
| Lab page 1 | 74 | 157 | 43 |
| Lab page 2 | 94 | 217 | 63 |
| Lab page 3 | 64 | 127 | 33 |
Thus do not compare the size of the two views by a count of nodes. Compare the fields that each view finds.
failure modes
| problem | cause | correction |
|---|---|---|
| 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
A page has a price with display: none. Which extraction finds this price?
- The extraction from the accessibility tree
- The DOM query
- Both extractions
- 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.
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.
A field is in the DOM extraction and not in the extraction from the tree. What does this difference show?
- The field is hidden text, or the site marks a real field wrongly.
- The field is always a real visible field.
- 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.
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.
- Save the code example in an empty folder of your lab.
- Write a JSON file with a list of two products, each with a name and a price.
- Give each price the class
priceand a text that starts with "Price:". - Add a price with the field
hiddenset to true. - Add a price with the field
displayset to "none" on its list item, not on the price. - Run the code with Node, the flag
--experimental-strip-types, and the file. - 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
- WAI-ARIA 1.2, W3C, standard, read 2026-11-20
- DOM Standard, WHATWG, standard, read 2026-11-20
- My model of the two views of a page and of the comparison of two extractions, 2026-11 (not public).
- Lab result of this course: the node counts of the DOM and the accessibility tree on 3 lab-built pages, 2026-11 (not public).