定时打卡小程序

定时打卡:

目录结构:
只需在自己的python项目下随便创建一个文件夹(下图中为:daka),然后将下载的chromedriver.exe、ask_for_leave.py、log.txt(此文件夹为空,保存运行程序时的日志信息,直接在文件夹下创建一个名为log.txt的文件夹即可)。
定时打卡小程序
chromedriver.exe
此文件是google浏览器的驱动文件,可在下载地址: http://chromedriver.storage.googleapis.com/index.html上选择与自己电脑上的google浏览器相同版本的驱动。

如何查看google浏览器版本,请查看此文章链接: https://blog.csdn.net/weixin_38792396/article/details/121489004

ask_for_leave.py(只需修改标注修改的两个地方)

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
import datetime
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options
def qinjia(browser,url):
    browser.get(url)
    sleep(1)
    browser.implicitly_wait(3)
    WebDriverWait(browser,5).until(EC.presence_of_all_elements_located((By.ID,"user_main")))
    user_main_div=browser.find_element_by_id("user_main")
    username_input=user_main_div.find_element_by_id("txtId")   #用户名
    password_input=user_main_div.find_element_by_id("txtMM")    #密码
    login_btn=user_main_div.find_element_by_id("IbtnEnter")   #登录按钮

    # 修改1:此处的账号和密码
    username_input.send_keys("==================账号===================")
    password_input.send_keys("==================密码===================")
    login_btn.click()

    sleep(1)
    browser.implicitly_wait(3)
    WebDriverWait(browser, 5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "tabThinM")))
    table_tag=browser.find_element_by_class_name("tabThinM")
    href_body=table_tag.find_elements_by_tag_name("tbody")[2]
    href=href_body.find_elements_by_tag_name("tr")[1].find_element_by_tag_name("a").get_attribute("href")
    browser.get(href)

    table_wjTA=browser.find_element_by_id("wjTA")

    div_gerenjiankang=table_wjTA.find_elements_by_class_name("dvO")[0]  #个人健康
    div_shenqing=table_wjTA.find_elements_by_class_name("dvO")[1]       #申请进入

    # 个人健康
    selects_tag=div_gerenjiankang.find_elements_by_tag_name("select")
    work_station_select=selects_tag[2]
    health_station_select=selects_tag[3]
    live_station_select=selects_tag[4]
    family_station_select=selects_tag[5]

    Select(work_station_select).select_by_value("1")
    Select(health_station_select).select_by_value("1")
    Select(live_station_select).select_by_value("1")
    Select(family_station_select).select_by_value("1")


    #申请进入
    select_shenqin_time_tags=div_shenqing.find_elements_by_tag_name("select")

    input_shenqin_reaseons_tags=div_shenqing.find_elements_by_tag_name("input")
    target_place_input=input_shenqin_reaseons_tags[0]
    reason_input=input_shenqin_reaseons_tags[1]
    # 修改2:成此处的申请目的地和事由
    target_place_input.send_keys("=====================申请目的========================")
    reason_input.send_keys("==========================事由===============================")

    Select(select_shenqin_time_tags[0]).select_by_value("2")
    Select(select_shenqin_time_tags[1]).select_by_value("11")
    Select(select_shenqin_time_tags[2]).select_by_value("3")
    Select(select_shenqin_time_tags[3]).select_by_value("19")

    submit_input=browser.find_element_by_tag_name("input")
    submit_input.click()


def log(message):
    curent_time = datetime.datetime.now()
    print(curent_time)

    f = open("log.txt", "a+", encoding="utf-8")

    f.write(str(curent_time) + ":  "+message+"\n")
    f.close()
def headLessChrome():
    chrome_driver = r"chromedriver.exe"
    chrome_options=Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-gpu")
    browser=webdriver.Chrome(options=chrome_options, executable_path=chrome_driver)
    return browser
if __name__ == '__main__':
    url="http://login.cuit.edu.cn/Login/xLogin/Login.asp"
    browser=headLessChrome()
    try:
        qinjia(browser,url)
        log("成功")
    except:
        log("失败")
    browser.quit()

log.txt
直接创建一个空的log.txt文件。

创建完毕之后,run一下ask_for_leave.py文件,即可运行一次。

如果想要每天自动打卡,完全解放双手,按照Win10怎么样设置每天定时运行某程序将run我们的ask_for_leave.py文件设置为win10的定时运行任务即可。

感谢技术支持@hello world之日白匠传送门

上一篇:sharepoint 2010 如何给文档库或自定义列表添加评论功能


下一篇:MySQL练习