無障礙功能
Accessibility 類別提供用於檢查 Chromium 無障礙樹狀結構的方法。無障礙樹狀結構供輔助技術使用,例如螢幕閱讀器或切換裝置。
無障礙功能是非常平台特定的事物。在不同的平台上,可能有不同的螢幕閱讀器,其輸出可能差異很大。
Chromium、Firefox 和 WebKit 的渲染引擎具有「無障礙樹狀結構」的概念,然後將其轉換為不同的平台特定 API。Accessibility 命名空間提供對此無障礙樹狀結構的存取權。
當從內部瀏覽器 AX 樹狀結構轉換為平台特定 AX 樹狀結構或由輔助技術本身轉換時,大部分無障礙樹狀結構都會被過濾掉。預設情況下,Playwright 會嘗試近似此過濾,僅公開樹狀結構中「有趣」的節點。
已棄用
snapshot
在 v1.9 之前新增擷取無障礙樹狀結構的目前狀態。傳回的物件代表頁面的根可存取節點。
Chromium 無障礙樹狀結構包含在大多數平台和大多數螢幕閱讀器上未使用的節點。Playwright 也會捨棄它們以獲得更易於處理的樹狀結構,除非將 interesting_only 設定為 false
。
用法
傾印整個無障礙樹狀結構的範例
- 同步
- 非同步
snapshot = page.accessibility.snapshot()
print(snapshot)
snapshot = await page.accessibility.snapshot()
print(snapshot)
記錄焦點節點名稱的範例
- 同步
- 非同步
def find_focused_node(node):
if node.get("focused"):
return node
for child in (node.get("children") or []):
found_node = find_focused_node(child)
if found_node:
return found_node
return None
snapshot = page.accessibility.snapshot()
node = find_focused_node(snapshot)
if node:
print(node["name"])
def find_focused_node(node):
if node.get("focused"):
return node
for child in (node.get("children") or []):
found_node = find_focused_node(child)
if found_node:
return found_node
return None
snapshot = await page.accessibility.snapshot()
node = find_focused_node(snapshot)
if node:
print(node["name"])
引數
-
從樹狀結構中修剪不有趣的節點。預設值為
true
。 -
root
ElementHandle (選用)#快照的根 DOM 元素。預設值為整個頁面。
傳回
- NoneType | Dict#
-
role
str角色。
-
name
str節點的人類可讀名稱。
-
節點的目前值 (如果適用)。
-
description
str節點的額外人類可讀描述 (如果適用)。
-
keyshortcuts
str與此節點關聯的鍵盤快速鍵 (如果適用)。
-
roledescription
str角色的另一種人類可讀替代方案 (如果適用)。
-
valuetext
str目前值的描述 (如果適用)。
-
disabled
bool節點是否已停用 (如果適用)。
-
expanded
bool節點是否已展開或摺疊 (如果適用)。
-
focused
bool節點是否已聚焦 (如果適用)。
-
modal
bool節點是否為強制回應 (如果適用)。
-
multiline
bool節點文字輸入是否支援多行 (如果適用)。
-
multiselectable
bool是否可以選取多個子項 (如果適用)。
-
readonly
bool節點是否為唯讀 (如果適用)。
-
required
bool節點是否為必要 (如果適用)。
-
selected
bool節點是否在其父節點中選取 (如果適用)。
-
checked
bool | "mixed"核取方塊是否已核取,或為「混合」 (如果適用)。
-
pressed
bool | "mixed"切換按鈕是否已核取,或為「混合」 (如果適用)。
-
level
int標題的層級 (如果適用)。
-
valuemin
float節點中的最小值 (如果適用)。
-
valuemax
float節點中的最大值 (如果適用)。
-
autocomplete
str控制項支援哪種自動完成 (如果適用)。
-
haspopup
str目前為節點顯示哪種彈出視窗 (如果適用)。
-
invalid
str此節點的值是否以及以何種方式無效 (如果適用)。
-
orientation
str節點是水平還是垂直方向 (如果適用)。
-
子節點 (如果有的話,如果適用)。
-