automation protocols and drivers · lesson 3 of 7 · sources checked 2026-11-20
worlds, frames, and where code runs
This lesson shows where code runs in a page, where a selector stops, and how a tool reads a result through the DOM.
status of this lesson: documented in my test systempurpose
A tool can run its own scripts in a page and still not see the variables of that page. The cause is the isolated world: it shares the elements of the page, but not its globals. A reader that looks in the wrong place returns "unknown", and the result of the page is lost.
objectives
- Explain why a reader in one world cannot see the globals of a different world.
- Name three places in a page where a selector or a script stops.
- Read a result through the DOM when the tool runs in an isolated world.
- Describe the traces that the work of a tool in an isolated world can leave.
new terms
- isolated world
- A script context that shares the DOM with the page but not the page's global variables.
- page frame
- The document of a frame element inside a page, with its own globals.
- realm
- A separate JavaScript global environment, for example an iframe or a worker.
- benchmark platform
- My test system. It runs browsers and tools against test pages and records the result.
- init script
- A script that a tool registers before the page scripts start, and that runs in each new document.
- selector
- An expression that finds elements in a DOM tree.
- shadow root
- A separate DOM subtree that a normal selector from the main document cannot enter.
- foreign page frame
- A page frame from a different scheme, host, or port.
- automation artifact
- A signal that an automation tool leaks and that a detector can read.
how it works
One page can hold more than one place where code runs. Each place has its own globals:
| place | globals | dom |
|---|---|---|
| Main world | The page globals | The DOM of the document |
| Isolated world | The globals of the tool | The same DOM of the same document |
| page frame | Its own globals | Its own document |
| Worker | Its own globals | No DOM |
two worlds, one dom
The main world holds the page scripts and their globals. An isolated world holds the scripts of a tool. Each world is a separate realm, with its own global object. But the two worlds see the same elements.
The W3C document of WebDriver BiDi gives one form of this. It states that "Each sandbox is a unique ECMAScript Realm". A sandbox "provides access to platform objects in an existing Window realm via SandboxProxy objects" W3C, source 1.
For example, a page script sets the global pageResult to pass. A tool script in an isolated world reads typeof pageResult, and gets undefined. The same tool script reads an element attribute on the body, and gets the value that the page wrote.
read a result through the dom
The result reader of the benchmark platform, my test system, reads the result of a page from a DOM element. It does not read a page global. A reader of a page global can return "unknown" when a tool runs its scripts in an isolated world.
Thus the contract between the page and the reader is one element and one element attribute. For example, the page writes data-result="pass" on the body element. Each world can read this element attribute.
init scripts and new documents
An init script is a script that a tool registers before the page scripts start. In my model, it runs in each document that opens after the registration. A document that is already open does not get it.
The BiDi document runs its preload scripts after the browser makes a new document in a navigable W3C, source 1. A navigable is the top page or a page frame. For example, a page frame that opens before the registration has no copy of the script. A tool that must have the script in each page frame registers it before the first navigation.
where a selector stops
A selector finds elements in one DOM tree. Three places stop a selector from the top document:
- A shadow root: a separate DOM subtree. For example, the selector
#pricedoes not find an element with that ID inside a shadow root. - A foreign page frame: a page frame from a different scheme, host, or port. It has its own document, and the top document cannot read into it.
- A worker: it is a separate realm with no DOM. A page global and an init script of the tool do not reach it.
A tool must open each of these places with its own step. For example, it reads the shadow root of a host element first, and then runs the selector inside it.
what a site can still see
A main-world probe does not see the globals of an isolated world. But the work of the tool goes through the shared DOM. For example, a click, a new element attribute, or a changed value is visible to each page script.
Thus the work of a tool can still leave an automation artifact, also when its globals stay hidden. The code example shows this trace.
the two sides
| automation side | detection side |
|---|---|
| A reader must use a shared surface, the DOM, when the tool runs in an isolated world. | Main-world probes do not see an isolated world. But the work that a tool does in the page can still leave traces in the DOM. |
design decisions
| option | gain | cost | when it is correct |
|---|---|---|---|
| Read the result from a DOM element | Each world can read it. | The page must write the element. The element is visible to page scripts. | Each tool that runs in an isolated world |
| Read the result from a page global | No change to the DOM | A reader in an isolated world gets "unknown". | Only a tool that runs in the main world |
| Register an init script before the first navigation | Each new document gets the script. | An open document does not get it. | Each script that must run before the page scripts |
| Open each shadow root and page frame with its own step | The selector finds the element. | More steps for each page | Pages with shadow roots or page frames |
diagram
where code runs in a page: worlds, the shared dom, page frames, and workers
- Main worldThe page scripts and the page globals. A page probe runs here.no result
- Isolated worldThe scripts of a tool, with their own globals. A main-world read does not see these globals.no result
- Shared DOMThe elements and the element attributes. Both worlds read and write them. A tool reads a result here.pass
- Shadow rootA separate DOM subtree. A normal selector from the main document does not enter it.fail
- Foreign page frameA different scheme, host, or port. Its own document and worlds. A selector from the top document stops here.fail
- WorkerA separate realm with no DOM. A page global and an init script of the tool do not reach it.fail
A result that a tool must read goes through the shared DOM of the same document.pass
code example
The example makes two worlds with the Node vm module. Both worlds share one synthetic DOM object. The page script writes a global and an element attribute. The reader in the isolated world reads both.
The DOM, the worlds, and the result value are synthetic. No part describes a real product.
Read a result through the DOM, because an isolated world does not see the globals of the page. Show the trace that the DOM value leaves.
// Synthetic model of two worlds in one page, with the Node vm module.
// Both worlds share one DOM object. Each world has its own globals.
const vm = require("node:vm");
// The argument is a synthetic result value of the page.
const value = process.argv[2] ?? "synthetic-pass";
// One shared DOM: one element with a data attribute.
const dom = { body: { dataset: {} } };
// The main world: the page script runs here.
const mainWorld = vm.createContext({ document: dom });
vm.runInContext(
`globalThis.pageResult = ${JSON.stringify(value)};
document.body.dataset.result = ${JSON.stringify(value)};
`,
mainWorld
);
// The isolated world: the tool reads here.
const isolatedWorld = vm.createContext({ document: dom });
const globalRead = vm.runInContext(
`typeof pageResult === "undefined" ? "unknown" : pageResult`,
isolatedWorld
);
const domRead = vm.runInContext(
`document.body.dataset.result ?? "unknown"`,
isolatedWorld
);
console.log(`main world global: ${mainWorld.pageResult}`);
console.log(`isolated read of the global: ${globalRead}`);
console.log(`isolated read of the DOM: ${domRead}`);
// The trace: the DOM value is also visible to each page script.
const pageView = vm.runInContext(
`document.body.dataset.result ?? "none"`,
mainWorld
);
console.log(`trace: a main-world script reads data-result=${pageView}`);output
The global in the main world, the two reads of the isolated world, and one trace line.
main world global: synthetic-pass
isolated read of the global: unknown
isolated read of the DOM: synthetic-pass
trace: a main-world script reads data-result=synthetic-passI ran the code with the Node runtime, with the argument synthetic-pass. The read of the global gives "unknown". The read of the DOM gives the value. The trace line shows that a page script can read the same value.
failure modes
| problem | cause | correction |
|---|---|---|
| The reader returns "unknown" on one tool. | The tool runs in an isolated world, and the reader reads a page global. | Read the result from a DOM element. |
| An init script does not run in one page frame. | The page frame opened before the registration of the script. | Register the script before the first navigation. Open the page again. |
| A selector finds no element, and the element is on the screen. | The element is in a shadow root or in a page frame. | Open the shadow root or the page frame first. Then run the selector. |
| A main-world probe reports no tool, and a tool runs in the page. | The tool keeps its globals in an isolated world. | Probe the changes in the shared DOM and the events too, not only the globals. |
| A value that the page script sets is missing in a worker. | The worker is a separate realm with no page globals. | Send the value to the worker in a message. |
check questions
Explain why a reader in an isolated world cannot see a global that the page script sets.
sample answer
Each world is a separate realm with its own global object. The page script sets the global in the main world. The isolated world shares the DOM with the page, but not the globals. Thus the reader gets no value, and it returns unknown.
Name three places in a page where a selector or a script from the top document stops.
sample answer
A shadow root, because it is a separate DOM subtree. A foreign page frame from a different scheme, host, or port, because it has its own document. A worker, because it is a separate realm with no DOM.
A tool runs its scripts in an isolated world. Where does the reader get the result of the page?
- From a global variable of the page
- From an element attribute of the body
- From a variable of a worker
answer
b. From an element attribute of the body
Both worlds share the DOM. A reader in an isolated world does not see the globals of the page, and a worker has no DOM of the page.
An init script does not run in one page frame. The page frame opened before the tool registered the script. What is the correction?
- Register the script before the first navigation
- Read the result from a page global
- Run the selector a second time
answer
a. Register the script before the first navigation
An init script runs in each document that opens after its registration. A document that is already open does not get it.
Describe how the work of a tool in an isolated world can leave a trace that a page script can read.
sample answer
The globals of the isolated world stay hidden from the main world. But the work of the tool goes through the shared DOM. A click, a new element attribute, or a changed value is visible to each page script.
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.
- Remove the line of the page script that writes
document.body.dataset.result. - Run the code with Node and the argument
synthetic-pass. - Record the two reads of the isolated world and the trace line.
- Put the line back, and run the code again.
- Write which read of the isolated world gives the value in each run.
- Write which run leaves a value that a page script can read.
sample answer
This sample answer uses the synthetic page of the code example.
Without the DOM write, both reads of the isolated world give "unknown", and the trace line shows "none". The global stays in the main world only. With the DOM write, the read of the DOM gives synthetic-pass, and the read of the global still gives "unknown". Only the second run leaves a value in the DOM, and a page script can read it.
sources
- WebDriver BiDi, W3C, draft, read 2026-11-20
- My model of the worlds, page frames, and workers of a page, 2026-11 (not public).
- Benchmark platform, code (my test system): the result reader reads a DOM element, not a page global, 2026-11 (not public).