学校健康系统自动打卡

1.脚本代码

代码其实很早就写完了,我把更多时间花在了测试和如何自动化上

主要是使用了selenium模拟鼠标的点击以及输入功能进行打卡

#引入selenium库中的 webdriver 模块
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time
import os
def main():
    chrome_driver = r'C:UsersASUSDesktopchromedriver.exe'#填自己的目录
    driver = webdriver.Chrome(executable_path=chrome_driver)
    driver.get('http://ehall.jit.edu.cn/new/index.html?tdsourcetag=s_pcqq_aiomsg')
    driver.maximize_window()#最大化窗口,方便容纳更多元素
    time.sleep(3)
    driver.find_element_by_xpath('//*[@id="ampHasNoLogin"]/span[2]').click()
    time.sleep(2)
    driver.find_element_by_xpath('//*[@id="username"]').send_keys('X')#你的学号
    driver.find_element_by_xpath('//*[@id="password"]').send_keys('X')#你的密码
    driver.find_element_by_xpath('//*[@id="login_form1"]/div[2]/input').click()
    time.sleep(5)
    driver.switch_to.window(driver.window_handles[0])#重定位当前窗口
    driver.find_element_by_xpath('//*[@id="widget-hot-01"]/div[1]/widget-app-item[3]/div/div/div[2]/div[1]').click()
    driver.switch_to.window(driver.window_handles[1])
    time.sleep(5)
    driver.find_element_by_xpath('/html/body/main/article/section/div[2]/div[1]').click()#新增
    driver.switch_to.window(driver.window_handles[1])
    time.sleep(2)
    # 选框1:体温
    driver.find_element_by_xpath('/html/body/div[11]/div/div[1]/section/div[2]/div/div[2]/div[2]/div[1]/div/div/div[2]/div/div/div[1]').click()
    driver.switch_to.window(driver.window_handles[1])
    time.sleep(1)
    driver.find_element_by_xpath('/html/body/div[17]/div/div/div/div[2]/div/div[3]/span').click()
    # 选框2:高危地区
    driver.find_element_by_xpath('/html/body/div[11]/div/div[1]/section/div[2]/div/div[2]/div[2]/div[4]/div/div/div[2]/div/div/div[1]').click()
    driver.switch_to.window(driver.window_handles[1])
    time.sleep(1)
    driver.find_element_by_xpath('/html/body/div[19]/div/div/div/div[2]/div/div[1]/span').click()
    # 选框3:健康码颜色
    driver.find_element_by_xpath('/html/body/div[11]/div/div[1]/section/div[2]/div/div[2]/div[2]/div[5]/div/div/div[2]/div/div/div[1]').click()
    driver.switch_to.window(driver.window_handles[1])
    time.sleep(1)
    driver.find_element_by_xpath('/html/body/div[20]/div/div/div/div[2]/div/div[2]/span').click()
    # 选框4:去过南京以外
    driver.find_element_by_xpath('/html/body/div[11]/div/div[1]/section/div[2]/div/div[2]/div[2]/div[6]/div/div/div[2]/div/div/div[1]').click()
    driver.switch_to.window(driver.window_handles[1])
    time.sleep(1)
    driver.find_element_by_xpath('/html/body/div[21]/div/div/div/div[2]/div/div[2]/span').click()
    #提交
    driver.switch_to.window(driver.window_handles[1])
    time.sleep(1)
    driver.find_element_by_xpath('/html/body/div[11]/div/div[2]/footer/div').click()
    driver.switch_to.window(driver.window_handles[1])
    time.sleep(1)
    driver.find_element_by_xpath('/html/body/div[28]/div[1]/div[1]/div[2]/div[2]/a[1]').click()
    print("提交成功")
    time.sleep(5)
if __name__ == '__main__':
    main()

2.使用必读:

一.运行前提
请安装selenium库,安装方法(如果没有安装pip请百度)
pip install selenium

二.请安装谷歌浏览器,下载对应driver
下载driver地址:http://chromedriver.storage.googleapis.com/index.html

路径的设置,将下载好的driver路径写入代码,例如:
chrome_driver = r'C:UsersASUSDesktopchromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver)

我的driver放在C:UsersASUSDesktopchromedriver.exe


三.用户与密码设置
代码:
driver.find_element_by_xpath('//*[@id="username"]').send_keys('你的学号')
driver.find_element_by_xpath('//*[@id="password"]').send_keys('你的密码')

四.选框4的修改

选框1-4分别对应

体温是否超过37度:否
是否去过高危地区:否
健康码颜色:绿色
14天内是否去过南京以外地区:是

如要把是修改为否,即14天内未离开南京,只要将最后一句话的div数组修改即可
driver.find_element_by_xpath('/html/body/div[11]/div/div[1]/section/div[2]/div/div[2]/div[2]/div[6]/div/div/div[2]/div/div/div[1]').click()
driver.switch_to.window(driver.window_handles[1])
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[21]/div/div/div/div[2]/div/div[2]/span').click()()

修改以后:
driver.find_element_by_xpath('/html/body/div[11]/div/div[1]/section/div[2]/div/div[2]/div[2]/div[6]/div/div/div[2]/div/div/div[1]').click()
driver.switch_to.window(driver.window_handles[1])
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[21]/div/div/div/div[2]/div/div[1]/span').click()//在这修改

五.双击脚本不会运行
请安装pipinstaller
将脚本转换成可执行程序
转换命令:
pyinstaller -F ******.py //这里写的是所在目录

之后便会在C:Users你的电脑用户名dist 文件夹中出现exe程序,此时可双击执行

或者直接cd到脚本所在目录,pyinstaller以后会在当前目录生成exe

六.执行失败怎么办
脚本中放置了很多time.sleep()语句,方便网页加载完成后寻找网页元素
若失败一定是sleep时间过短的结果(网络因人而异),请修改为较大的值

3.如何准时打卡

这个我研究了很久,学校网每晚11点会断,但是打卡是每天0点开始的,到早上才会来网

于是最好的打卡时间在早7点以后

我们打开控制面板>管理工具>任务计划系统

创建任务,配置常规、触发器、操作

路径为pyinstaller打包好的exe所在路径

可以了,之后建议把电脑的锁屏和休眠时间改为从不,笔记本盖关闭时操作改为不做操作

我设置每天7.15打卡,然后又添加了7.40自动关机的操作

这样就不需要自己关电脑,每天晚上睡觉合上盖子但是电脑还在运行,到早上自动打卡自动关机

成功的演示结果:

打卡程序显示我在每天早上7:15进行了打卡,但那时候我还在睡觉

原文地址:https://www.cnblogs.com/echoDetected/p/13766468.html