windows下实现校园网自动登陆
需求
校园网经常会断开连接,每次都要重新登录,windows定时任务自动登录校园网
思路
1.python脚本检测网络是否连接,如果没网络,实现校园网账号密码自动登录
2.将脚本加入windows任务计划程序中,定时启动
实现过程
1.下载Chrome浏览器以及chromedriver(和chrome版本一致)
chrome各个版本下载地址
http://npm.taobao.org/mirrors/chromedriver/
https://chromedriver.storage.googleapis.com/index.html
将chrome加入环境变量(C:\Users\HP\AppData\Local\Google\Chrome\Application)
2.python代码 (C:/lss/autologin/auto_login.py)
#-*- coding:utf8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
import os,subprocess
username = "102111153"
password = "1207371X"
url = "http://202.114.177.246/srun_portal_pc?ac_id=1&theme=pro"
def isNotConnecting():
# 0 表示有网 1 2表示错误
res = subprocess.call("ping www.baidu.com",shell=True)
return True if res else False
def Signon():
driver = webdriver.Chrome()
driver.get(url)
# 通过使用选择器选择到表单元素进行模拟输入和点击按钮提交
driver.find_element(By.ID, 'username').click()
driver.find_element(By.ID, 'username').clear()
driver.find_element(By.ID, 'username').send_keys(username)
driver.find_element(By.ID, 'password').click()
driver.find_element(By.ID, 'password').clear()
driver.find_element(By.ID, 'password').send_keys(password)
driver.find_element(By.ID, 'login-account').click()
# 退出窗口
driver.close()
if __name__ == "__main__":
if isNotConnecting():
Signon()
3.将脚本加入windows定时任务
更改用户为“SYSTEM”,为了让脚本每次自动执行时,不会有cmd以及浏览器弹出来影响工作
步骤:名称=>更改用户或组=>高级=>立即查找
查看python路径
import sys
print(sys.path)
C:\Users\HP\AppData\Local\Programs\Python\Python310\python.exe
C:/lss/autologin/auto_login.py
最后,右键该列任务,可以立即运行,然后查看运行结果。