add main prog

This commit is contained in:
2026-02-14 10:10:15 -05:00
parent 69f0ee184a
commit f50b7dab84
2 changed files with 77 additions and 0 deletions

74
main.py Normal file
View File

@@ -0,0 +1,74 @@
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.edge.options import Options
# 如果你安装了 webdriver_manager取消下面这行的注释
# from webdriver_manager.microsoft import EdgeChromiumDriverManager
import requests
def is_connected():
try:
# 尝试访问一个极其稳定的网站,设置短超时
requests.get("https://www.google.com", timeout=3)
return True
except:
return False
def auto_login_wifi():
# 配置 Edge 选项
edge_options = Options()
edge_options.add_argument("--start-maximized") # 最大化窗口
edge_options.add_argument("--ignore-certificate-errors") # 忽略证书错误公共Wi-Fi常见
edge_options.add_argument("--headless") # 如果不想看到浏览器界面,取消注释这一行
try:
# 初始化 Edge 驱动
# 如果安装了 webdriver_manager使用下面这行
# driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()), options=edge_options)
# 如果没有安装 webdriver_manager且驱动已在环境变量中直接使用下面这行
driver = webdriver.Edge(options=edge_options)
print("正在打开浏览器并访问触发页面...")
# 1. 访问触发页面,这会自动重定向到 OSU 的登录页
driver.get("http://captive.apple.com/")
# 设置显式等待,最长等待 20 秒,确保页面元素加载出来
wait = WebDriverWait(driver, 20)
print("等待跳转至登录页面...")
# 2. 定位并勾选 "I accept the terms of use"
# 使用 name="visitor_accept_terms" 定位,这比包含随机数字的 ID 更稳定
checkbox = wait.until(EC.element_to_be_clickable((By.NAME, "visitor_accept_terms")))
if not checkbox.is_selected():
checkbox.click()
print("已勾选同意协议。")
# 3. 定位并点击 "Log In" 按钮
# 同样避开动态 ID使用 XPath 查找 value 为 "Log In" 的提交按钮
login_button = driver.find_element(By.XPATH, "//input[@type='submit' and @value='Log In']")
login_button.click()
print("已点击登录按钮。")
# 保持窗口打开一小会儿以确认连接成功(根据需要调整)
import time
time.sleep(5)
print("操作完成。")
except Exception as e:
print(f"发生错误: {e}")
finally:
# 如果你想让浏览器保持打开状态,注释掉下面这行
driver.quit()
if __name__ == "__main__":
if is_connected():
print("网络已连接,无需登录。")
else:
auto_login_wifi()

3
run.bat Normal file
View File

@@ -0,0 +1,3 @@
@echo off
cd /d "C:\Users\yuanz\src\osu-wifi-login"
uv run main.py