Mac 使用AppleScript实现Chrome浏览器操作自动化

Mac自带的AppleScript脚本可以实现Chrome浏览器操作自动化,模拟手动操作程度能达到9成多。
基本上能想到的操作都能模拟到,除了有一些扩展程序不太好通过JavaScript来控制,剩下的只要能在控制台查看元素,就可以通过JavaScript找DOM元素来模拟操作。再配合AppleScript对Mac系统和部分系统内软件的操作,写个Chrome操作自动化应该不成问题。
  1. 设置Chrome浏览器允许AppleScript发送JavaScript代码命令。
    Mac 使用AppleScript实现Chrome浏览器操作自动化_第1张图片
  2. 研究有哪些AppleScript脚本命令可以控制Chrome浏览器。打开[脚本编辑器]app,打开资源库窗口,点击加号添加Chrome,查看资源库中Chrome的相关脚本指令文档。
    Mac 使用AppleScript实现Chrome浏览器操作自动化_第2张图片
    Mac 使用AppleScript实现Chrome浏览器操作自动化_第3张图片
    Mac 使用AppleScript实现Chrome浏览器操作自动化_第4张图片
  3. 编写具体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('b_searchbox')[0].click()"
				delay 1
			end tell
		end tell
		
		--在搜索框中键入搜索词
		tell application "System Events"
			keystroke "AppleScript"
			delay 1
			keystroke return
			delay 1
		end tell
		
		tell window 1
			tell active tab
				--点击搜索按钮(搜索)
				execute javascript "document.getElementsByClassName('b_searchboxSubmit')[0].click()"
				delay 3
				--选中搜索结果页的第一条数据的某个能响应click()的元素
				execute javascript "document.getElementsByClassName('b_algo')[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].click()" --例如 <a target="_blank" href="https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html" h="ID=SERP,5148.1" class=""><i data-bm="40"><i class="01">Introduction</i> <i>to</i> <i>AppleScript</i> <i class="01">Language</i> <i class="01">Guide</i></i></a>
				delay 3
			end tell
		end tell
	end tell
end chromeTestClickElements

(*
	--通过ID获取元素(getElementById)tell tab
	--execute javascript "document.getElementById('vkeyIcon').click()"
	--通过类名获取元素(getElementsByClassName)
	--execute javascript "document.getElementsByClassName('identicon__address-wrapper')[0].click()"
	--通过name属性获取元素(getElementsByName)
	--execute javascript "document.getElementsByName('identicon__address-wrapper')[0].click()"
	--通过标签名获取元素(getElementsByTagName)
	--execute javascript "document.getElementsByTagName('identicon__address-wrapper')[0].click()"
	--?
	--execute javascript "document.querySelectorAll('.tile-grid .most-visited')[3].click()"
	--set active tab index to 3 --切换当前window中选中的标签 tell window
	--让input标签聚焦,即模拟点击input
	--execute javascript "document.getElementsByClassName('name-input')[0].focus()"
	--获取input元素的value值
	--set grabVar to execute javascript "document.getElementsByClassName('readonly-input__input')[0].value;" --可用,这种写法可以从外部接收到value值
	--通过JS中的DOM相关方法来获取文本节点的值 可以在Chrome控制台来尝试打印元素内容或者模拟click点击,Console控制台可以提示代码,非常方便
	set grabVar to execute javascript "document.getElementsByClassName('c-nav--footer__listitem--meta v--center')[0].childNodes[0].innerHTML" 
*)

(*
	--从这篇文章找到的Chrome如果用AppleScript控制
	--https://apple.stackexchange.com/questions/81062/applescript-click-automation-in-google-chrome-browser#:~:text=Applescript%20click%20automation%20in%20Google%20Chrome%20browser%3F%201,with%20the%20words%20%22start-up%22%20next%20to%20it.%20
	tell application "Google Chrome" to tell active tab of window 1
		execute javascript "document.getElementById('su').click()"
		--execute javascript "document.querySelectorAll('.tile-grid .most-visited')[3].click()"
	end tell
*)
  1. 可借助Chrome的console控制台,直接测试寻找合适的JavaScript代码。
    Mac 使用AppleScript实现Chrome浏览器操作自动化_第5张图片

GitHub Demo地址

参考资料:
菜鸟教程:HTML DOM 教程

不嫌弃的可以加一下微信一起讨论交流
Mac 使用AppleScript实现Chrome浏览器操作自动化_第6张图片

你可能感兴趣的:(AppleScript,Mac)