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:\Users\ASUS\Desktop\chromedriver.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:\Users\ASUS\Desktop\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver)
我的driver放在C:\Users\ASUS\Desktop\chromedriver.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進行了打卡,但那時候我還在睡覺