2021-01-07 11:46:05 -08:00
# class: Accessibility
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-03-03 14:56:28 -08:00
* langs: csharp, js, python
2022-11-23 08:40:47 -08:00
* deprecated: This class is deprecated. Please use other libraries such as [Axe ](https://www.deque.com/axe/ ) if you need to test page accessibility. See our Node.js [guide ](https://playwright.dev/docs/accessibility-testing ) for integration with Axe.
2022-08-09 12:26:43 -07:00
2021-01-07 11:46:05 -08:00
The Accessibility class provides methods for inspecting Chromium's accessibility tree. The accessibility tree is used by
assistive technology such as [screen readers ](https://en.wikipedia.org/wiki/Screen_reader ) or
[switches ](https://en.wikipedia.org/wiki/Switch_access ).
Accessibility is a very platform-specific thing. On different platforms, there are different screen readers that might
have wildly different output.
2021-03-26 18:47:16 +01:00
Rendering engines of Chromium, Firefox and WebKit have a concept of "accessibility tree", which is then translated into different
2021-01-14 15:01:39 -08:00
platform-specific APIs. Accessibility namespace gives access to this Accessibility Tree.
2021-01-07 11:46:05 -08:00
2021-01-14 15:01:39 -08:00
Most of the accessibility tree gets filtered out when converting from internal browser AX Tree to Platform-specific AX-Tree or by
2021-01-07 11:46:05 -08:00
assistive technologies themselves. By default, Playwright tries to approximate this filtering, exposing only the
"interesting" nodes of the tree.
## async method: Accessibility.snapshot
2022-07-05 16:24:50 -08:00
* since: v1.8
2022-11-23 08:40:47 -08:00
* deprecated: This method is deprecated. Please use other libraries such as [Axe ](https://www.deque.com/axe/ ) if you need to test page accessibility. See our Node.js [guide ](https://playwright.dev/docs/accessibility-testing ) for integration with Axe.
2021-01-07 11:46:05 -08:00
- returns: < [null]|[Object]>
- `role` < [string]> The [role ](https://www.w3.org/TR/wai-aria/#usage_intro ).
- `name` < [string]> A human readable name for the node.
- `value` < [string]|[float]> The current value of the node, if applicable.
- `description` < [string]> An additional human readable description of the node, if applicable.
- `keyshortcuts` < [string]> Keyboard shortcuts associated with this node, if applicable.
- `roledescription` < [string]> A human readable alternative to the role, if applicable.
- `valuetext` < [string]> A description of the current value, if applicable.
- `disabled` < [boolean]> Whether the node is disabled, if applicable.
- `expanded` < [boolean]> Whether the node is expanded or collapsed, if applicable.
- `focused` < [boolean]> Whether the node is focused, if applicable.
- `modal` < [boolean]> Whether the node is [modal ](https://en.wikipedia.org/wiki/Modal_window ), if applicable.
- `multiline` < [boolean]> Whether the node text input supports multiline, if applicable.
- `multiselectable` < [boolean]> Whether more than one child can be selected, if applicable.
- `readonly` < [boolean]> Whether the node is read only, if applicable.
- `required` < [boolean]> Whether the node is required, if applicable.
- `selected` < [boolean]> Whether the node is selected in its parent node, if applicable.
- `checked` < [boolean]|"mixed"> Whether the checkbox is checked, or "mixed", if applicable.
- `pressed` < [boolean]|"mixed"> Whether the toggle button is checked, or "mixed", if applicable.
- `level` < [int]> The level of a heading, if applicable.
- `valuemin` < [float]> The minimum value in a node, if applicable.
- `valuemax` < [float]> The maximum value in a node, if applicable.
- `autocomplete` < [string]> What kind of autocomplete is supported by a control, if applicable.
- `haspopup` < [string]> What kind of popup is currently being shown for a node, if applicable.
- `invalid` < [string]> Whether and in what way this node's value is invalid, if applicable.
- `orientation` < [string]> Whether the node is oriented horizontally or vertically, if applicable.
- `children` < [Array]< [Object]>> Child nodes, if any, if applicable.
Captures the current state of the accessibility tree. The returned object represents the root accessible node of the
page.
2021-01-12 12:14:27 -08:00
:::note
2021-01-14 07:48:56 -08:00
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 [`option: interestingOnly` ] is set to `false` .
2021-01-12 12:14:27 -08:00
:::
2021-01-07 11:46:05 -08:00
2022-11-21 10:40:21 -08:00
**Usage**
2021-01-07 11:46:05 -08:00
An example of dumping the entire accessibility tree:
```js
const snapshot = await page.accessibility.snapshot();
console.log(snapshot);
```
2021-02-25 22:03:39 -08:00
```java
String snapshot = page.accessibility().snapshot();
System.out.println(snapshot);
```
2021-01-14 07:48:56 -08:00
```python async
snapshot = await page.accessibility.snapshot()
print(snapshot)
```
```python sync
snapshot = page.accessibility.snapshot()
print(snapshot)
```
2021-03-03 19:39:32 +01:00
```csharp
2021-05-12 10:30:22 +02:00
var accessibilitySnapshot = await page.Accessibility.SnapshotAsync();
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(accessibilitySnapshot));
2021-03-03 19:39:32 +01:00
```
2021-01-07 11:46:05 -08:00
An example of logging the focused node's name:
```js
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);
2022-03-23 10:50:35 +00:00
if (foundNode)
return foundNode;
2021-01-07 11:46:05 -08:00
}
return null;
}
```
2021-03-03 19:39:32 +01:00
```csharp
2021-05-12 10:30:22 +02:00
var accessibilitySnapshot = await page.Accessibility.SnapshotAsync();
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(accessibilitySnapshot));
2021-03-03 19:39:32 +01:00
```
2021-02-25 22:03:39 -08:00
```java
// FIXME
String snapshot = page.accessibility().snapshot();
```
2021-01-14 07:48:56 -08:00
```python async
def find_focused_node(node):
2023-06-27 11:53:27 +02:00
if node.get("focused"):
2021-01-14 07:48:56 -08:00
return node
for child in (node.get("children") or []):
found_node = find_focused_node(child)
2023-06-27 11:53:27 +02:00
if found_node:
2022-03-23 10:50:35 +00:00
return found_node
2021-01-14 11:09:44 -08:00
return None
2021-01-14 07:48:56 -08:00
snapshot = await page.accessibility.snapshot()
node = find_focused_node(snapshot)
if node:
2021-01-14 15:01:39 -08:00
print(node["name"])
2021-01-14 07:48:56 -08:00
```
```python sync
def find_focused_node(node):
2023-06-27 11:53:27 +02:00
if node.get("focused"):
2021-01-14 07:48:56 -08:00
return node
for child in (node.get("children") or []):
found_node = find_focused_node(child)
2023-06-27 11:53:27 +02:00
if found_node:
2022-03-23 10:50:35 +00:00
return found_node
2021-01-14 11:09:44 -08:00
return None
2021-01-14 07:48:56 -08:00
snapshot = page.accessibility.snapshot()
node = find_focused_node(snapshot)
if node:
2021-01-14 15:01:39 -08:00
print(node["name"])
2021-01-14 07:48:56 -08:00
```
2021-02-03 12:19:25 -08:00
## async method: Accessibility.snapshot
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-03-03 19:39:32 +01:00
* langs: java
2021-02-03 12:23:02 -08:00
- returns: < [null]|[string]>
2021-02-03 12:19:25 -08:00
2021-05-22 20:29:32 -07:00
## async method: Accessibility.snapshot
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-05-22 20:29:32 -07:00
* langs: csharp
2021-09-09 11:39:26 +02:00
- returns: < [null]|[JsonElement]>
2021-05-22 20:29:32 -07:00
2021-01-07 11:46:05 -08:00
### option: Accessibility.snapshot.interestingOnly
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- `interestingOnly` < [boolean]>
Prune uninteresting nodes from the tree. Defaults to `true` .
### option: Accessibility.snapshot.root
2022-07-05 16:24:50 -08:00
* since: v1.8
2021-01-07 11:46:05 -08:00
- `root` < [ElementHandle]>
2022-03-23 10:50:35 +00:00
The root DOM element for the snapshot. Defaults to the whole page.