ATOMac簡介
atomac是一個支持在mac上做自動化的python庫,GitHub地址如下:
https://github.com/pyatom/pyatom
安裝
# Python2 sudo easy_install atomac # Python3 pip3 install git+https://github.com/pyatom/pyatom/
使用
1. 啟動程序
import atomac atomac.launchAppByBundleId('com.apple.Automator')
查看bundleID的方法
在應用程序->右鍵選擇包內容->Contents->Info.plist
2. 獲取Window
automator = atomac.getAppRefByBundleId('com.apple.Automator')
window = automator.windows()[0]
print(window)
輸出
<atomac.AXClasses.NativeUIElement AXWindow '未命名'>
3. 獲取應用標題
print(window.AXTitle)
輸出
未命名
4. 查看元素
atomac支持獲取和操作大部分的元素,可以使用xcode提供的accessibility inspector快速查看各個元素
路徑: Xcode -> Open Developer Tools -> Accessibility inspector
Atomac支持的元素類型有:
textAreas
textFields
buttons
windows
sheets
staticTexts
genericElements
groups
radioButtons
popUpButtons
rows
sliders
atomac所有的定位方法加上'R'字符,就變成了一個搜索方法(可以添加額外的搜索條件)
5. 獲取元素
通過快照我們可以進行元素定位, 這里我們以關閉按鈕為例
closeButton = window.buttonsR('關閉')[0] print(closeButton)
輸出:
<atomac.AXClasses.NativeUIElement AXButton '關閉'>
6. 條件搜索元素
atomac支持findFirstR方法,根據屬性來進行元素搜索,例如
closeButton = window.findFirstR(AXRole='AXButton', AXTitle='關閉')
支持的屬性可以在Accessibility inspector中查看
findFirstR方法返回首個匹配的元素, 如果沒有找到匹配的元素則返回空列表
findAllR使用方法相同,返回所以匹配的元素列表,沒有匹配的元素則返回空列表
7. 查看元素支持的屬性
closeButton = window.findFirstR(AXRole='AXButton', AXTitle='關閉') print(closeButton.getAttributes())
輸出
['AXRole', 'AXHelp', 'AXEnabled', 'AXWindow', 'AXSize', 'AXTitle', 'AXRoleDescription', 'AXTopLevelUIElement', 'AXFocused', 'AXParent', 'AXPosition', 'AXFrame', 'AXIdentifier']
查看屬性值
print(closeButton.AXTitle)
輸出
關閉
8. 查看元素支持的操作
print(closeButton.getActions())
輸出
['Press']
9. 元素操作
closeButton.Press()
任何支持的操作都可以這樣調用