Skip to main content

Accessibility

Accessibility 类提供了检查 Chromium 的可访问性树的方法。可访问性树由 屏幕阅读器switches 等辅助技术使用。

¥The Accessibility class provides methods for inspecting Chromium's accessibility tree. The accessibility tree is used by assistive technology such as screen readers or switches.

可访问性是一个非常特定于平台的东西。在不同的平台上,有不同的屏幕阅读器,可能会产生截然不同的输出。

¥Accessibility is a very platform-specific thing. On different platforms, there are different screen readers that might have wildly different output.

Chromium、Firefox 和 WebKit 的渲染引擎都有 "可达性树" 的概念,然后将其转换为不同平台特定的 API。可访问性命名空间提供对此可访问性树的访问。

¥Rendering engines of Chromium, Firefox and WebKit have a concept of "accessibility tree", which is then translated into different platform-specific APIs. Accessibility namespace gives access to this Accessibility Tree.

当从内部浏览器 AX 树转换为特定于平台的 AX 树或通过辅助技术本身时,大多数可访问性树都会被过滤掉。默认情况下,Playwright 尝试近似此过滤,仅公开树的 "interesting" 节点。

¥Most of the accessibility tree gets filtered out when converting from internal browser AX Tree to Platform-specific AX-Tree or by assistive technologies themselves. By default, Playwright tries to approximate this filtering, exposing only the "interesting" nodes of the tree.


已弃用

¥Deprecated

snapshot

Added in: v1.8 accessibility.snapshot
已弃用

此方法已被弃用。如果你需要测试页面可访问性,请使用其他库,例如 Axe。请参阅我们的 Node.js guide 以了解与 Axe 的集成。

¥This method is deprecated. Please use other libraries such as Axe if you need to test page accessibility. See our Node.js guide for integration with Axe.

捕获可访问性树的当前状态。返回的对象表示页面的根可访问节点。

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

注意

Chromium 可访问性树包含大多数平台和大多数屏幕阅读器未使用的节点。Playwright 也会丢弃它们,以便更容易处理树,除非 interestingOnly 设置为 false

¥The Chromium accessibility tree contains nodes that go unused on most platforms and by most screen readers. Playwright will discard them as well for an easier to process tree, unless interestingOnly is set to false.

用法

¥Usage

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

¥An example of dumping the entire accessibility tree:

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

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

¥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);
if (foundNode)
return foundNode;
}
return null;
}

参数

¥Arguments

从树中修剪掉不感兴趣的节点。默认为 true

¥Prune uninteresting nodes from the tree. Defaults to true.

快照的根 DOM 元素。默认为整个页面。

¥The root DOM element for the snapshot. Defaults to the whole page.

返回

¥Returns

role.

节点的人类可读名称。

¥A human readable name for the node.

节点的当前值(如果适用)。

¥The current value of the node, if applicable.

节点的附加人类可读描述(如果适用)。

¥An additional human readable description of the node, if applicable.

与此节点关联的键盘快捷键(如果适用)。

¥Keyboard shortcuts associated with this node, if applicable.

人类可读的角色替代方案(如果适用)。

¥A human readable alternative to the role, if applicable.

当前值的描述(如果适用)。

¥A description of the current value, if applicable.

节点是否被禁用(如果适用)。

¥Whether the node is disabled, if applicable.

节点是否展开或折叠(如果适用)。

¥Whether the node is expanded or collapsed, if applicable.

节点是否获得焦点(如果适用)。

¥Whether the node is focused, if applicable.

节点是否为 modal(如果适用)。

¥Whether the node is modal, if applicable.

节点文本输入是否支持多行(如果适用)。

¥Whether the node text input supports multiline, if applicable.

是否可以选择多个子级(如果适用)。

¥Whether more than one child can be selected, if applicable.

节点是否为只读(如果适用)。

¥Whether the node is read only, if applicable.

是否需要该节点(如果适用)。

¥Whether the node is required, if applicable.

是否在其父节点中选择该节点(如果适用)。

¥Whether the node is selected in its parent node, if applicable.

是否选中该复选框,或 "mixed"(如果适用)。

¥Whether the checkbox is checked, or "mixed", if applicable.

是否选中切换按钮,或 "mixed"(如果适用)。

¥Whether the toggle button is checked, or "mixed", if applicable.

标题级别(如果适用)。

¥The level of a heading, if applicable.

节点中的最小值(如果适用)。

¥The minimum value in a node, if applicable.

节点中的最大值(如果适用)。

¥The maximum value in a node, if applicable.

控件支持哪种类型的自动补齐(如果适用)。

¥What kind of autocomplete is supported by a control, if applicable.

当前正在为节点显示哪种类型的弹出窗口(如果适用)。

¥What kind of popup is currently being shown for a node, if applicable.

该节点的值是否以及以何种方式无效(如果适用)。

¥Whether and in what way this node's value is invalid, if applicable.

节点是水平方向还是垂直方向(如果适用)。

¥Whether the node is oriented horizontally or vertically, if applicable.

子节点(如果有)(如果适用)。

¥Child nodes, if any, if applicable.