基于selenium库的扬州大学广陵学院校园网自动连接脚本

写在前面

        本人为扬州大学广陵学院在校生一枚。

        这是一款本人设计的,针对于本学校,扬州大学广陵学院的自动连接校园网的脚本程序。

        实在是懒得每次开机连这个破校园网。

        最好把校园网设置成自动连接,要不然有的时候开机自动连不上。

 还是建议给这个放桌面,防止有时候校园网抽抽突然断了,这个程序点两下就连上了反正。

附上下载连接

https://wwm.lanzouj.com/iDpvO21hux8h
密码:69sg

文件夹中的readme.txt为使用的教程。

如果觉得好用,希望给个赞咯,谢谢!

源码

以下附上python脚本源代码,相互交流学习!

#导入包
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service
import os
from time import sleep
from win11toast import toast
from playsound import playsound
from webdriver_manager.microsoft import EdgeChromiumDriverManager
import os
import re

def get_edge_driver_version():
    # Edge 浏览器的默认安装路径
    edge_installation_path = r"C:\Program Files (x86)\Microsoft\Edge\Application"

    # 查找 Edge 浏览器的版本号
    edge_version_regex = re.compile(r'\d+\.\d+\.\d+\.\d+')
    edge_version = None

    try:
        for folder_name in os.listdir(edge_installation_path):
            # 确保目录名称符合 Edge 版本的命名约定
            if re.match(edge_version_regex, folder_name):
                edge_version = folder_name
                break
    except FileNotFoundError:
        print("Edge 浏览器未安装或安装路径不正确")

    return edge_version

if __name__ == "__main__":
    edge_driver_version = get_edge_driver_version()
    if edge_driver_version:
        print("Edge 浏览器驱动版本:", edge_driver_version)
    else:
        print("未找到 Edge 浏览器安装路径或 Edge 浏览器未安装")

#获取当前Edge浏览器版本
print('正在初始化系统环境配置')
import win32api

def get_file_version(file_path):
    try:
        info = win32api.GetFileVersionInfo(file_path, '\\')
        ms = info['FileVersionMS']
        ls = info['FileVersionLS']
        file_version = f"{win32api.HIWORD(ms)}.{win32api.LOWORD(ms)}.{win32api.HIWORD(ls)}.{win32api.LOWORD(ls)}"
        return file_version
    except Exception as e:
        return f"无法获取文件版本信息:{e}"

# 指定要获取版本信息的exe文件路径
exe_edge_path = 'msedge.exe'  # 举例:Windows记事本的路径


# 调用函数获取文件版本信息
versionweb= get_file_version("C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")
versiondrive = get_edge_driver_version()
print(f"{exe_edge_path} 的文件版本是:{versionweb}")
print(f'当前驱动版本为{versiondrive}')
drive_path = 'C:\Program Files (x86)\Microsoft\Edge\Application'

#检查当前驱动版本与edge版本是否一致
if versiondrive == versionweb:
    print('驱动文件正常,正在添加到path环境中')
    # 将驱动文件地址添加到系统环境变量的PATH中
    os.environ['PATH'] += os.pathsep + os.path.dirname(drive_path)
    print('添加配置文件成功')
    
else:
        # 下载匹配的Edge驱动文件
    print('不存在所需的驱动文件,正在下载配置')
    new_driver_path = EdgeChromiumDriverManager().install()
    os.environ['PATH'] += os.pathsep + os.path.dirname(new_driver_path)
    print('添加配置文件成功!')

def read():
    i = 0
    try:
        f = open("STD\STD.ini", encoding='utf-8')
        line = f.readline()
        list = ['', '', '']
        while line:
            list[i] = line.split('=')[1].replace('\n', '')
            line = f.readline()
            i = i + 1
        f.close()
        print("配置文件正常")
    except Exception as e:
        print("配置文件错误")
        print("except:", e)
    return list

list = read()
try:
    edge_options = Options()
    # 使用无头模式
    edge_options.add_argument('--headless')
    # 禁用GPU,防止无头模式出现莫名的BUG
    edge_options.add_argument('--disable-gpu')
    
    driver = webdriver.Edge(options=edge_options)

    #打开url
    driver.get("http://10.0.98.1/a70.htm")
    #输入账号
    name = driver.find_element(By.XPATH, '//*[@id="edit_body"]/div[3]/div[1]/form/input[2]')
    #输入密码
    password = driver.find_element(By.XPATH, '//*[@id="edit_body"]/div[3]/div[1]/form/input[3]')
    name.send_keys(list[1])
    password.send_keys(list[2])
    #点击确定按钮
    element = driver.find_element(By.XPATH, '//*[@id="edit_body"]/div[3]/div[1]/form/input[1]')
    driver.execute_script("arguments[0].click();", element)
    #关闭浏览器
    sleep(1)
    driver.quit()
except Exception as e:
    playsound('music\fault.mp3')
    toast("WARNING!", "主人,连接失败了啦喵~(๑•́ ₃ •̀๑)エー",
        icon = r'D:/code/Python/自动访问校园网demo/ab3x1-abx8v-001.ico')
    sleep(1)
    driver.exit()
    os._exit(0)
else:
    playsound('music\Success.mp3')
    toast("Successful!", "您已经成功连接上校园网啦喵~(๑•́ ₃ •̀๑)エー",
           icon = r'D:/code/Python/自动访问校园网demo/ab3x1-abx8v-001.ico')

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值