完整代碼:
1 from apscheduler.schedulers.blocking import BlockingScheduler 2 from selenium import webdriver 3 from time import sleep 4 import time 5 6 c_minute = "42" #設置 分鍾 7 c_hour = "8,17" #設置 小時 8 c_week = "mon-fri" #設置 日 9 10 def work(): 11 try: 12 driver = webdriver.Chrome() 13 driver.get("輸入你的網址") 14 #填充用戶名 密碼 驗證碼 15 driver.find_element_by_id("userNameTNJ").send_keys("用戶名") #再chrome中安F12查找到你輸入用戶名文本框的 id 或者class
: 用find_element_by_class_name 查找class 16 driver.find_element_by_id("password").send_keys("密碼") 17 driver.find_element_by_class_name('btn_skin').click() #找到你所需要點擊按鈕的class或者id 然后點擊 18 # sleep(3) 19 # driver.quit() 20 print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ": Clock Success!") #time.strftime格式化日期函數,獲得當前系統時間 21 except: 22 print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ": Clock Filed!") 23 24 if __name__ == '__main__': 25 # 添加任務 26 scheduler = BlockingScheduler() 27 # 設置定時任務時間 28 scheduler.add_job(work, 'cron', minute=c_minute, hour=c_hour, day_of_week=c_week) 29 print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ": Add Task Work!") 30 try: 31 scheduler.start() 32 except (KeyboardInterrupt, SystemExit): 33 scheduler.shutdown()2019-01-23