python+selenium簡(jiǎn)易的定時(shí)自動(dòng)辦公室打卡

背景:近期因公司要求,需每日打卡,分別是早上八點(diǎn)半之前打卡,下午5點(diǎn)后打卡。

打卡形式: 打卡的形式可以是網(wǎng)站上打開(kāi),也可以手機(jī)軟件打卡。

目的:避免忘記打卡。于是我為了避免忘記打卡,在公司電腦上寫(xiě)了一個(gè)腳本,準(zhǔn)時(shí)打卡。

首先準(zhǔn)備好環(huán)境:Windows,python3.9,selenium, 下載好webdriver。

第一步:登陸。進(jìn)入目標(biāo)網(wǎng)頁(yè),填入賬號(hào)密碼,點(diǎn)擊登陸后返回瀏覽器對(duì)象給打卡函數(shù)。

def login():
    browser = webdriver.Chrome(chrome_driver) #chrome_options=chrome_options
    browser.get('http://183.62.37.18:88/wui/index.html')
    time.sleep(3)
    acc=browser.find_element_by_css_selector('#loginid')
    acc.clear()
    pw=browser.find_element_by_css_selector("#userpassword")
    pw.clear()
    acc.send_keys('xxxxxx')
    pw.send_keys('xxxxx')
    browser.find_element_by_css_selector('#submit').click()
    time.sleep(3)
    browser.refresh()
    return browser

第二步,打卡。點(diǎn)擊打開(kāi)打卡頁(yè)面,判斷是否已經(jīng)打開(kāi),做打卡或者更新操作。

def workoff(browser):
    time.sleep(5)
    if len(browser.find_elements_by_css_selector('.checkTitle.text-elli'))==0:
        browser.find_element_by_css_selector('#signPlugin > div').click()
        time.sleep(1)
    time.sleep(2)
    if len(browser.find_elements_by_partial_link_text('更新打卡時(shí)間'))==1:
        renew=browser.find_element_by_partial_link_text('更新打卡時(shí)間')
        if int(date[17:19]) > 9 and int(date[17:19]) <= 16 :
            renew.click() 
            print('更新加班時(shí)間')
        print('已經(jīng)打卡了,刷新打卡時(shí)間完成')
        browser.quit()
    else:
        if len(browser.find_elements_by_css_selector('.signBtnPanel button'))==1:
            first=browser.find_element_by_css_selector('.signBtnPanel button')
            first.click()
            browser.quit()
            print('首次打卡成功')

第三步,檢查打開(kāi)時(shí)間。首先是判斷是否為工作日,其次是判斷是否為打卡時(shí)間,如果不是多久之后重新嘗試打卡。

判斷是否為工作日使用:

from chinese_calendar import is_workday
date = datetime.datetime.now().date()
is_workday(date) (判斷是否為工作日)

判斷是否當(dāng)日是否為打卡時(shí)間:

只需判斷 date = datetime.datetime.now().date()
if date[17:19]<9, 即可。時(shí) 如果大于0小于9,即在早上8點(diǎn)和下午5點(diǎn)之江

以下是我粗糙的邏輯判斷:設(shè)定為早上8點(diǎn)到8點(diǎn)30分打卡,下午5點(diǎn)30分到6點(diǎn)打卡;
打卡后,等待特定時(shí)間后,將再次打卡。

def checktime():
    date = datetime.datetime.now().date()
    while True:
        if is_workday(date):
            while True:
                # browser = webdriver.Chrome(chrome_driver) #chrome_options=chrome_options

                # print("是工作日")
                response=requests.get('https://www.baidu.com/')
                date=response.headers['Date']  # 線上GMT時(shí)間

                target_time=date[0:17]+'00:25:00 GMT'#上班時(shí)間 
                target_time2=date[0:17]+'09:35:00 GMT'#下午班時(shí)間
                print(date)
                if date[0:20]==target_time[0:20]: #八點(diǎn)開(kāi)始才能打卡;
                    if int(date[20:22])<30:
                        browser=login()
                        workoff(browser)
                        print('開(kāi)始等待9.5個(gè)小時(shí)后打卡')
                        time.sleep(34200) #9.5個(gè)小時(shí)后
                        
                        continue
                

                if date[0:20]==target_time2[0:20]:#五點(diǎn)之后都能打卡
                    if int(date[20:22])>30:
                        browser=login()
                        workoff(browser)
                        print('開(kāi)始等待14.5小時(shí)后打卡')
                        time.sleep(52200) #14.5小時(shí)后
                        
                        continue
                    else:
                        print('開(kāi)始等待半小時(shí)打卡')
                        time.sleep(1800)
                        
                        continue

                if date[0:17]==target_time2[0:17]:  
                    if int(date[17:19])>int(target_time2[17:19]):
                        browser=login()
                        workoff(browser) 
                        h=24-int(date[17:19])  #判斷小時(shí)差
                        d=60-int(date[20:22])  #判斷分鐘差
                        if d>30:
                            s=h*60*60
                        else:
                            s=(h-0.5)*60*60
                        print('開(kāi)始等待{}小時(shí)后打開(kāi)'.format(s/60/60))
                        time.sleep(s)
                        
                        continue


                print('非打卡時(shí)間,打卡時(shí)間為早上八點(diǎn),下午五點(diǎn)半之后都可以')
                if 0 < int(date[17:19]) and int(date[17:19]) < 9:
                    h=9-int(date[17:19])  # 1-8之間
                    s=h*60*60
                    print('開(kāi)始等待{}小時(shí)后打開(kāi)'.format(s/60/60))
                    time.sleep(s)
                    
                    continue
                else:
                    continue

        else:
            print("是休息日")
            print('24小時(shí)后嘗試打卡')
            time.sleep(86400) #24小時(shí)后嘗試打卡
            continue

之后,在考慮用request的形式,直接發(fā)送打卡信息進(jìn)行打卡。下面是api從客戶端獲取的信息,構(gòu)造post請(qǐng)求,直接在云服務(wù)器運(yùn)行腳本,定時(shí)打卡即可,下次試試。

form_data={
'date': '2021-06-29',
'belongtime': '17:00',
'type': 'off',
'canSignTime': '00:00',
'across': '0',
'belongdate': '2021-06-29',
'datetime': '2021-06-29 17:00:00',
'min': '420',
'islastsign': '1',
'signSectionTime': '2021-06-30 00:00:59',
'signSectionBeginTime': '2021-06-29 09:30:00',
'isYellow': '1',
'isPunchOpen': '1',
'isacross': '0',
'pre': '0',
'signSection': '2021-06-29 09:30:00',#2021-06-30 00:00:59
#'signbelongspan':'今天'
'active': '1',
'canSignBeginTime': '09:30',
'needSign': '1',
'workmins': '450',
'min_next': '450',
'serialid': '1',
'signAcross': '1',
'signAcross_next': '0',
'time': '17:00', 
'position':"" ,
'longitude':"" ,
'latitude': "",
'sid': "",
'mac': "",
'deviceId': "",
'deviceInfo':"" }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容