文章目录
一、介绍
本文主要讲解如何使用AppleScript+JavaScript脚本实现校园网自动认证。
二、实现过程
1.打开AppleScript 编辑器
2.代码
2.1登陆脚本
tell application "Safari"
activate
make new document with properties {URL:"http://10.3.130.12"}--打开校园网
delay 1 -- 暂停 1 秒
tell document 1
do JavaScript "
document.getElementById('username').value=188xxxxxxxx;--学号
var s = document.getElementById('domain');
s.options[1].selected = true;//刷新页面
s.options[2].selected = true;
document.getElementById('password').value=xxxxxx;--密码
document.getElementById('login').click();
"
end tell
delay 1
close document 1
end tell
say "Wi-Fi连接成功"
2.2退出脚本
tell application "Safari"
activate
make new document with properties {URL:"http://10.3.130.12"}
delay 0.5 -- 暂停 0.5 秒
tell document 1
do JavaScript "document.getElementById('logout').click();"
end tell
delay 0.5
close document 1
end tell
3.结果
实现自动打开浏览器,自动输入用户名密码和选择营业厅,完成登陆后自动关闭浏览器。
三、代码讲解和常见问题
1.代码讲解
1.1前面这一部分是用AppleScript打开safari浏览器并转到校园网登陆页面。
tell application "Safari"
activate
make new document with properties {URL:"http://10.3.130.12"}--打开校园网
delay 1 -- 暂停 1 秒
tell document 1
1.2然后嵌入一段JavaScript代码(因为AppleScript无法实现抓取页面内容的操作),s.options[1].selected有两行是因为我们学校的校园网中国移动那个option是已经selected的,但是页面不显示,经过反复试验,可以先选择中国电信再选择中国移动。
do JavaScript "
document.getElementById('username').value=188xxxxxxxx;--学号
var s = document.getElementById('domain');
s.options[1].selected = true;//刷新页面
s.options[2].selected = true;
document.getElementById('password').value=xxxxxx;--密码
document.getElementById('login').click();
"
1.3执行完毕之后关闭当前页面。
delay 1
close document 1
2.常见问题
2.1安全与隐私
- 打开权限
在系统偏好设置->安全与隐私->辅助功能,要打开脚本编译器才能够顺利运行脚本。
2.2Safari开发选项设置
- 打开偏好设置
- 显示“开发”菜单
- 允许Apple事件中的JavaScript