selenium 实现自动登录校网(网页版)

selenium 实现自动登录校网

学校使用的服务器使用校园网认证登录连接网络,时不时会断网,每次断网都得跑到机房去重新登录。去年我采用 selenium + 定时指令写了一个可以自动登录的程序,但是前几天不能用了,报了个错误:

AttributeError: 'WebDriver' object has no attribute 'find_element_by_id'

检查之后,发现是 selenium 版本更新导致原来的函数不存在了,现在统一了函数格式为find_element(By.属性)。修改之后,已经可以正常使用了。

趁这个机会,我把实现的方法分享给大家。

1、安装 python3.7

selenium 只能在 python3 使用,所以先安装 python3

# 安装依赖
yum install -y openssl-devel openssl-static zlib-devel lzma tk-devel xz-devel bzip2-devel ncurses-devel gdbm-devel readline-devel sqlite-devel gcc libffi-devel

# 安装 python3
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
tar -xvf Python-3.7.0.tgz
mv Python-3.7.0 /usr/local
cd /usr/local/Python-3.7.0/
./configure
make & make install

# 生成软链接
ln -s /usr/local/Python-3.7.0/python /usr/bin/python3

服务器默认 python 的版本是 python2,我们需要手动修改默认的 python 版本

# 设置默认python版本
vi ~/.bash_profile
# 添加内容
alias python="/usr/bin/python3"
alias pip="/usr/local/bin/pip3"
# 重启bash
source ~/.bash_profile

2、安装 selenium

安装 python3 的自动化库 selenium,更多使用方法可以去查阅相关资料。

# 安装selenium,使用豆瓣源,并添加信任
pip install selenium -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

3、安装 chrome 和驱动

selenium 需要浏览器驱动,这里以 chrome 驱动为例

首先,安装 chrome,下面提供两种方法,根据自己的需要会进行选择

# 方法一:输入命令
curl https://intoli.com/install-google-chrome.sh | bash

# 方法二:
vim /etc/yum.repos.d/google-chrome.repo
# 添加信息
[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-sl.google.com/linux/linux_signing_key.pub
# 查询可用版本(实时更新)
yum list | grep chrome  
yum -y install 谷歌浏览器版本 --nogpgcheck

# 测试安装是否成功
google-chrome --headless --disable-gpu --screenshot https://www.baidu.com

其次,安装 chrome dirver,一定要确保安装的 chrome dirver 与安装的 chrome 版本是一致的

# 首先查看 chrome 的版本
google-chrome --version
# 访问:https://chromedriver.storage.googleapis.com/index.html,下载对应的内核驱动
# 注意:这里浏览器的版本与浏览器驱动版本的匹配,不能照抄
wget https://chromedriver.storage.googleapis.com/浏览器版本/chromedriver_linux64.zip
unzip chromedriver_linux64.zip

4、定时任务

定时任务的实现,使用 crontab -e 指令进入编辑界面后,每一行都代表一项任务,每行的每个字段代表一项设置,对应的设置如下:minute hour day month week command

输入自己要执行的定时任务内容,我需要则要输入的内容是:

1 6,12,18 * * * python ~/daily/login.py

5、实现代码

编写代码时,不能照搬,打开对应的网站,打开开发者工具,对照编写。

# -*- coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By

# 如果设置定时任务的是 root,则需要这段代码,关闭沙盒,具体自行查阅相关资料
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')

# 浏览器驱动的路径要设置成绝对路径,否则有一定几率报错
driver = webdriver.Chrome(executable_path="/root/daily/chromedriver", chrome_options=options)
driver.get("校园网登录地址")
# 网页源代码
# print(driver.page_source)
# 从浏览器的地址栏中读取当前URL
print(driver.current_url)
# 从浏览器中读取当前页面标题
print(driver.title)

print('开始登录')
driver.find_element(By.ID, 'VipDefaultAccount').send_keys('账号')
driver.find_element(By.ID, 'VipDefaultPassword').send_keys('密码')
driver.find_element(By.ID, 'login').click()
print('登录成功')

driver.quit()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值