AppleScript+JavaScript自动认证校园网

一、介绍

本文主要讲解如何使用AppleScript+JavaScript脚本实现校园网自动认证。
登陆界面

二、实现过程

1.打开AppleScript 编辑器

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
    允许Apple事件中的JavaScript
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值