电脑需预先安装好python(包括相应python库)和浏览器驱动chromedriver

  1. 在电脑任意位置创建下面两个文件:
1
2
3
4
5
6
7
8
9
### 文件1:filename1.bat ###
@echo off
if "%1"=="hide" goto CmdBegin
start mshta vbscript:createobject("wscript.shell").run("""%~0"" hide",0)(window.close)&&exit
:CmdBegin
#以上部分为设置bat运行时不显示窗口
set ssid="***校园网名称***"
netsh wlan connect name=%ssid%
python filename2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
### 文件2:filename2.py ###
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
import time

opt=Options()
opt.add_argument("--headless")

if __name__ =="__main__":
driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe",opt)
driver.get('http://10.211.220.9/0.htm') #修改为校园网认证网页的网址
driver.find_element_by_id("username").send_keys("***用户名***") #个人校园网用户名
s=Select(driver.find_element_by_id("suffix"))
s.select_by_value("2")
driver.find_element_by_id("password").send_keys("***密码***") #个人校园网密码
driver.find_element_by_xpath('//input[@type="submit"][@value="登 录"]').click() #通过xpath定位校园网认证网页中的登录按钮
#休息2s,防止登录失败
time.sleep(0.5)
driver.quit()
  1. WIN+R,输入taskschd.msc回车,打开任务计划程序,创建任务。

尝试后发现本文代码确实能够实现联网操作,但每次开机时其执行效果不好,无法及时有效运行。