macos - macOS Accessibility API - 查询 Books.app 时不支持 kAXErrorAttributeUnsupported
问题描述
我想递归查询 Books.app 的子元素以获取所有可点击元素。
但是,我注意到我似乎无法AXChildren
从 Books 窗口元素中获取属性的值。
您可以在 Playgrounds 中试用的代码片段:
import Cocoa
guard let books = NSWorkspace.shared.runningApplications.first(where: { $0.bundleIdentifier == "com.apple.iBooksX" }) else {
fatalError()
}
let booksPid = books.processIdentifier
let booksElement = AXUIElementCreateApplication(booksPid)
var bookRole: AnyObject?
let bookRoleError = AXUIElementCopyAttributeValue(booksElement, "AXRole" as CFString, &bookRole)
print("query book role error: \(bookRoleError.rawValue)")
print("book role: \(bookRole)")
var bookFocusedWindow: AnyObject?
let bookFocusedWindowError = AXUIElementCopyAttributeValue(booksElement, "AXFocusedWindow" as CFString, &bookFocusedWindow)
print("book focused window error: \(bookFocusedWindowError.rawValue)")
print("book focused window: \(bookFocusedWindow)")
guard let focusedWindowElement = bookFocusedWindow as! AXUIElement? else {
fatalError()
}
var windowRole: AnyObject?
let windowRoleError = AXUIElementCopyAttributeValue(focusedWindowElement, "AXRole" as CFString, &windowRole)
print("windowRole error: \(windowRoleError.rawValue)")
print("windowRole: \(windowRole)")
var children: AnyObject?
let childrenError = AXUIElementCopyAttributeValue(focusedWindowElement, "AXChildren" as CFString, &children)
print("children error: \(childrenError.rawValue)")
print("children: \(children)")
输出:
query book role error: 0
book role: Optional(AXApplication)
book focused window error: 0
book focused window: Optional(<AXUIElement 0x7fc6acd28280> {pid=17149})
windowRole error: 0
windowRole: Optional(AXWindow)
children error: -25205
children: nil
-25205
错误代码与属性不支持的代码匹配。
我检查了窗口元素支持的属性列表,AXChildren 就是其中之一。
var windowAttributes: CFArray?
let windowAttributesError = AXUIElementCopyAttributeNames(focusedWindowElement, &windowAttributes)
print("query window attributes error: \(windowAttributesError.rawValue)")
print("window attributes: \(windowAttributes)")
...
AXProxy,
AXDefaultButton,
AXMinimized,
AXChildren,
AXRole,
AXParent,
AXTitleUIElement,
AXCancelButton,
AXModal,
...
这种行为真的很奇怪,因为 Accessibility Inspector 显示了 window 元素的子元素:
解决方案
原来解决方案是使用AXUIElementCopyAttributeValues
. AXUIElementCopyAttributeValue
不过,除此之外的所有应用程序都为我工作。
var children: CFArray?
let childrenError = AXUIElementCopyAttributeValues(focusedWindowElement, "AXChildren" as CFString, 0, 999, &children)
print("children error: \(childrenError.rawValue)")
print("children: \(children)")
推荐阅读
- f# - 如何在记录上禁用 ToString
- python - Selenium WebDriver:Firefox 未按标签名称获取元素
- java - 带有计数和持续时间的 Storm 中的翻滚窗口
- azure - 具有自定义域的用户的 Azure AD B2B 身份验证错误 - AADSTS65005:当前不支持使用应用程序“我的应用程序”
- java - 使用 java6 从 Tomcat6 调用 JAX-WS RI 2.2 服务
- edi - 有没有办法在 837 中定位分段线?
- angular - 如果输入为空,则禁用按钮
- css - 如何使用 calc() 在颜色值之间切换?
- html - 为什么文本会超出 div?
- mysql - 如果某列中的值重复,则从设置结果中检索一行