Skip to main content
Version: 24.38.0

Accessibility.snapshot() 方法

🌐 Accessibility.snapshot() method

捕获辅助功能树的当前状态。返回的对象表示页面的根辅助节点。

🌐 Captures the current state of the accessibility tree. The returned object represents the root accessible node of the page.

语法

🌐 Signature

class Accessibility {
snapshot(options?: SnapshotOptions): Promise<SerializedAXNode | null>;
}

参数

🌐 Parameters

范围

🌐 Parameter

类型

🌐 Type

描述

🌐 Description

options

快照选项

(可选)

🌐 (Optional)

返回:

Promise<SerializedAXNode | null>

代表快照的 AXNode 对象。

🌐 An AXNode object representing the snapshot.

附注

🌐 Remarks

注意 Chrome 可访问性树包含在大多数平台和大多数屏幕阅读器上未使用的节点。Puppeteer 也会丢弃它们,以便获得更易处理的树,除非将 interestingOnly 设置为 false

示例 1

🌐 Example 1

转储整个可访问性树的示例:

🌐 An example of dumping the entire accessibility tree:

const snapshot = await page.accessibility.snapshot();
console.log(snapshot);

示例 2

🌐 Example 2

记录焦点节点名称的示例:

🌐 An example of logging the focused node's name:

const snapshot = await page.accessibility.snapshot();
const node = findFocusedNode(snapshot);
console.log(node && node.name);

function findFocusedNode(node) {
if (node.focused) return node;
for (const child of node.children || []) {
const foundNode = findFocusedNode(child);
return foundNode;
}
return null;
}