系统:Ubuntu22.04
selenium4和之前的版本不一样,在网上找的代码就报错说某某函数被弃用了。。。
步骤一:驱动问题
就是要下载个启动自己浏览器的驱动,我用的是chrome
chrome驱动下载
火狐的自已搜吧
下载完直接放在脚本对应的文件夹下面把,方便直接用
步骤二:上代码
import time
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import datetime
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
username_str = "xxx" # 你的校园网登陆用户名
password_str = "xxx" # 你的校园网登陆密码
CHROMEDRIVER_PATH = './chromedriver'
URL_SCHOOL_NET = 'xxx'
def login():
try:
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service, options=options)
driver.get(url=URL_SCHOOL_NET)
time.sleep(3)
driver.find_element(By.NAME,'f1').find_element(By.NAME,'DDDDD').send_keys(username_str)
driver.find_element(By.NAME,'f1').find_element(By.NAME, 'upass').send_keys(password_str)
print("成功输入用户名和密码 ; )")
time.sleep(2)
login_button = driver.find_element(By.XPATH,"//input[@name='0MKKey' and contains(@class, 'edit_lobo_cell')]")
driver.execute_script("arguments[0].click();"

本文介绍了一段使用Python的Selenium4库在Ubuntu22.04上自动登录校园网的脚本,包括处理Chrome驱动、登录函数和异常处理。脚本中涉及了Chrome的headless模式以适应系统启动时的无界面环境,以及对网络连接状态的检测。在系统服务中配置该脚本以实现在网络启动后自动连接校园网。
最低0.47元/天 解锁文章
1348

被折叠的 条评论
为什么被折叠?



