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
范围 | 类型 | 描述 |
---|---|---|
options | (可选的) |
Returns:
Promise<SerializedAXNode | null>
代表快照的 AXNode 对象。
¥An AXNode object representing the snapshot.
备注
¥Remarks
注意 Chrome 辅助功能树包含大多数平台和大多数屏幕阅读器都未使用的节点。Puppeteer 也会丢弃它们,以便更容易处理树,除非 interestingOnly
设置为 false
。
¥NOTE The Chrome accessibility tree contains nodes that go unused on most platforms and by most screen readers. Puppeteer will discard them as well for an easier to process tree, unless interestingOnly
is set to 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;
}