Mac自带的AppleScript脚本可以实现Chrome浏览器操作自动化,模拟手动操作程度能达到9成多。
基本上能想到的操作都能模拟到,除了有一些扩展程序不太好通过JavaScript来控制,剩下的只要能在控制台查看元素,就可以通过JavaScript找DOM元素来模拟操作。再配合AppleScript对Mac系统和部分系统内软件的操作,写个Chrome操作自动化应该不成问题。
- 设置Chrome浏览器允许AppleScript发送JavaScript代码命令。
- 研究有哪些AppleScript脚本命令可以控制Chrome浏览器。打开[脚本编辑器]app,打开资源库窗口,点击加号添加Chrome,查看资源库中Chrome的相关脚本指令文档。
- 编写具体AppleScript脚本代码。
tell application "System Events"
delay 1
--要在Chrome-视图-开发者,勾选允许Apple事件中的JavaScript
-- Chrome测试点击元素方法
my chromeTestClickElements()
end tell
-- Chrome测试点击元素方法
on chromeTestClickElements()
tell application "Google Chrome"
delay 2
activate --让app进入活跃状态
delay 2
--打开必应搜索网页
tell front window
make new tab with properties {
URL:"https://cn.bing.com/?FORM=BEHPTB&ensearch=1"}
delay 3
end tell
--让搜索页的搜索框聚焦
tell window 1
tell active tab
execute javascript "document.getElementsByClassName(