使用selenium实现自动登录南通大学校园网
使用selenium实现自动登录南通大学校园网
使用selenium模拟登录校园网
准备事项
python环境,selenium包,pyinstaller包,相应的浏览器驱动
源码
# -*- coding: UTF-8 -*-
'''
@File: netConnect.py
@IDE: PyCharm
@Author: yuchun
@Date: 2021/11/3 13:37
'''
import sys
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
broswer = webdriver.Edge(executable_path="C:\msedgedriver.exe")#浏览器驱动,我的是edge浏览器驱动
broswer.get('http://210.29.79.141/?isReback=1')#校园网网址
#time.sleep(1) 要是网页加载慢不能用可以试着加上这句代码,等待1s
try:
webChoser = broswer.find_element(By.XPATH, '//*[@id="edit_body"]/div[2]/div[5]/span[3]/input')
broswer.execute_script("arguments[0].click();", webChoser)
except Exception:
broswer.quit()
sys.exit()
userName = broswer.find_element(By.XPATH, '//*[@id="edit_body"]/div[2]/div[2]/div/div[2]/div[1]/div/form/input[3]')
userName.send_keys('xxxx')#填入你的账号
password = broswer.find_element(By.XPATH, '//*[@id="edit_body"]/div[2]/div[2]/div/div[2]/div[1]/div/form/input[4]')
password.send_keys('xxx')#填入你的密码
startBox = broswer.find_element(By.XPATH, '//*[@id="edit_body"]/div[2]/div[2]/div/div[2]/div[1]/div/form/input[2]')
broswer.execute_script("arguments[0].click();", startBox)
broswer.quit()#关闭浏览器
尝试过程中发现两个input虽然能定位但是click失败,应该是有什么别的元素在上面,用了broswer.execute_script("arguments[0].click();", startBox)这句实现点击操作
浏览器驱动地址
生成可执行exe文件
安装pyinstaller包
pip install pyinstaller
注意要先切到文件路径
在命令行输入
pyinstaller -F -w netConnect.py
成功后可以找到dist文件夹,里面有生成的exe文件
到这里点击exe文件就已经可用使用了
生成脚本文件
新建txt文件,复制代码,修改路径为你生成的python的exe文件路径,保存,修改后缀为.bat
@echo off
if "%1" == "yes" goto begin
mshta vbscript:createobject("wscript.shell").run("cmd /c C:\netConnect.exe",0)(window.close)&&exit
:begin
开机自启动
将bat文件放入开机启动文件夹
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
完成
效果:开机自启动还没有qq自启动快,有点不行==