提问人:mukishiki 提问时间:11/3/2023 更新时间:11/3/2023 访问量:14
如果 AXUIElement 的角色是 AXWebArea,如何获取 AXUIElement
How can I get AXUIElement if its role is AXWebArea
问:
我遇到了一个奇怪的问题。
我使用 AXUIElement 在 Google 中获取了 focusedUIElement,效果很好。但是当我第二天再次运行相同的代码时,它不起作用。
经过比较,我记得我上次启动了辅助功能检查器,但这次没有。所以我启动了它,然后代码又工作了。
出于这个原因,我递归查询了每个子元素,发现如果我没有启动辅助功能检查器,则子元素中没有返回其角色为 AXWebArea 的元素。
代码停止工作的问题仅在重新启动 Mac 后发生,而不是在退出辅助功能检查器时发生。
有没有人遇到过这个问题?
有关我的 Mac 的信息如下: MacBook Air (苹果 M1) macOS 14.0的 Xcode代码15.0.1
答: 暂无答案
评论
swift let systemPtr = AXUIElementCreateSystemWide() var timer: Timer? func applicationDidFinishLaunching(_ aNotification: Notification) { // TODO: Requesting Accessibility permissions timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in self.findFocusElement() } }
swift func findFocusElement() { if let focusedElement = systemPtr.getValue(.focusedUIElement) { let element = focusedElement as! AXUIElement let role = element.getValue(.role) as? String ?? "" debugPrint(role) } else { debugPrint("no find") } }
swift extension AXUIElement { func getValue(_ attribute: NSAccessibility.Attribute) -> AnyObject? { var value: AnyObject? let result = AXUIElementCopyAttributeValue(self, attribute.rawValue as CFString, &value) guard result == .success else { return nil } return value } }