class: Accessibility
Accessibility 类提供了检查 Chromium 的可访问性树的方法。辅助技术使用辅助功能树,例如 screen readers 或 switches。
可访问性是非常特定于平台的事情。在不同的平台上,会有不同的屏幕阅读器,它们的输出可能完全不同。
Blink (Chrome 的渲染引擎) 具有 “可访问性树” 的概念,然后将其转换为不同的平台特定的 API。可访问性命名空间为用户提供了访问 “眨眼可访问性树”。
从眨眼 AX 树转换为特定于平台的 AX 树或使用辅助技术本身时,大多数可访问性树都会被过滤掉。 默认情况下,Puppeteer 尝试近似此过滤,仅公开树的 “有趣” 节点。
accessibility.snapshot([options])#
options
<[Object]>interestingOnly
<[boolean]> 从树中修剪不感兴趣的节点。默认为true
。root
<[ElementHandle]> 快照的根 DOM 元素。默认为整个页面。
- returns: <[Promise]<[Object]>> An [AXNode] object with the following properties:
role
<[string]> The role.name
<[string]> 节点的可读名称。value
<[string]|[number]> 节点的当前值。description
<[string]> 与此节点关联的键盘快捷键。keyshortcuts
<[string]> 角色的可读替代。roledescription
<[string]> 当前值的描述。valuetext
<[string]> 当前值的描述。disabled
<[boolean]> 是否禁用节点。expanded
<[boolean]> 节点是展开还是折叠。focused
<[boolean]> 节点是否聚焦。modal
<[boolean]> Whether the node is modal.multiline
<[boolean]> 节点文本输入是否支持多行。multiselectable
<[boolean]> 是否可以选择多个孩子。readonly
<[boolean]> 节点是否为只读。required
<[boolean]> 是否需要该节点。selected
<[boolean]> 是否在其父节点中选择了该节点。checked
<[boolean]|"mixed"> 该复选框是选中还是 “混合”。pressed
<[boolean]|"mixed"> 切换按钮是选中还是 “混合”。level
<[number]> 标题级别。valuemin
<[number]> 节点中的最小值。valuemax
<[number]> 节点中的最大值。autocomplete
<[string]> 控件支持哪种自动控制完成。haspopup
<[string]> 当前正在为节点显示哪种弹出窗口。invalid
<[string]> 此节点的值是否无效以及以何种方式无效。orientation
<[string]> 节点是水平还是垂直。children
<[Array]<[Object]>> Child [AXNode]s of this node, if any.
捕获可访问性树的当前状态。返回的对象表示页面的根可访问节点。
NOTE Chromium 可访问性树包含大多数平台上未使用的节点,并且
大多数屏幕阅读器。 Puppeteer 也会丢弃它们,以便于处理树,除非interestingOnly
设置为false
。
转存整个可访问性树的例子:
const snapshot = await page.accessibility.snapshot();
console.log(snapshot);
记录焦点节点名称的例子:
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;
}
Blink 是 Chrome 的渲染引擎,它有一个 “可访问性树” 的概念,然后被转换成不同的平台特定的 API 。辅助功能命名空间允许用户访问 Blink 辅助功能树。
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。