基于python+selenium自動健康打卡的實現代碼
尊敬的老師,我忘了這次的健康打卡,反思的時候我想了很多東西,反省了很多事情,自己也很懊悔,觸犯了學校的規定,深刻認識到自己所犯錯誤的嚴重性… 卡!那是小學生才有的檢討。作為一個有點懶的人,對于每次的健康打卡,都是做著重復性的填寫,這讓本人很是頭疼,那就找找止疼藥吧
使用的工具需要有一定的python,html基礎,和實踐能力(畢竟實踐出真知,實踐能力強,你可以忽略前兩個,你是最棒的!):
Pycharm ,在pycharm官網里面下載社區版或專業版(沒其他的用途推薦用社區版); Python 我使用的是python3.8,在官網下載,可參考python安裝,配置好環境變量方便cmd 裝包; Selenium在cmd輸入 pip install selenium ,分布式自動化測試工具,用于模擬用戶在瀏覽器的行為; Chromedriver 這個是在pycharm里面驅動Chrome瀏覽器,下載后解壓放在Script下面,Chromedriver需要和自己的谷歌瀏覽器版本相對應置于桌面 將你寫好的py文件放在桌面或者你設置開機自啟動管理運行該程序就可以了
代碼中必要的地方都進行了注釋,注釋的多的地方就是我停留較久的地方,,,實習自動打卡的功能算是實現了,但還是有一些問題,比如說網絡延時,當網頁還未加載出來,你就無法定位該html元素,該程序就無法正常執行完操作,可以發一封郵件提醒你補填。歡迎大家共同探討遇到的問題或者文章中有其他不足之處還望雅正。
import timefrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.action_chains import ActionChains# 腳本自動登錄該網頁driver = webdriver.Chrome()# 將健康表的地址copy過來就行driver.get('你的健康表地址')time.sleep(2)print('開始點擊----立即登錄')# 第一次點擊登錄跳轉driver.find_element_by_xpath(’//*[@id='header-login-btn']’).click()time.sleep(5)print('開始點擊快速登錄')# # selenium判斷元素是否可以點擊或者處理# element = driver.find_element_by_id('img_out_191736586')# element = driver.find_element_by_xpath(’//*[@id='img_out_191733686']’)# bianji = element.is_enabled() #是否可以編輯,或者按鈕是否可以點擊# xinashi = element.is_displayed() #:判斷元素是否顯示# xunazhong = element.is_selected() #:判斷元素是否選中狀態# print(bianji,xinashi,xunazhong)# element.click()# 想了想為啥定位不到那個快速登錄元素,原來html知識不夠,切進iframedriver.switch_to.frame(0) # 1.用frame的index來定位,第一個是0# driver.switch_to.frame('frame1') # 2.用id來定位# driver.switch_to.frame('myframe') # 3.用name來定位# driver.switch_to.frame(driver.find_element_by_tag_name('iframe')) # 4.用WebElement對象來定位# 快捷登錄driver.find_element_by_xpath(’//*[@id='img_out_1917336586']’).click()# driver.find_elements_by_class_name(’img_out_focus’)[0].click()time.sleep(15)print('點擊大數據人工智能一班SHEET')# driver.find_elements_by_class_name('sheet-tab-name')[2].click()driver.find_element_by_xpath(’//*[@id='sheetbar']/div[2]/div[3]/div/div[4]/span’).click()time.sleep(3)print('開始點擊更多')driver.find_element_by_xpath(’//*[@id='toobarMoreButton']/div/div/div[1]’).click()# driver.find_element_by_class_name('toolbar-menu-button-more toolbar-inline-block').click()time.sleep(10)print('點擊搜索')driver.find_elements_by_xpath(’//*[@id='sheet-search-button']/div/div/div’)[1].click()time.sleep(3)print('開始點擊搜索框')getinput = driver.find_element_by_xpath(’//*[@id='search-panel-input']’)getinput.send_keys('北極熊')time.sleep(2)print('已獲取該位置' + getinput.location)time.sleep(2)# Key.tab選擇,Kys.enter確定結束driver.find_element_by_id(’alloy-simple-text-editor’).click()driver.find_element_by_id(’alloy-simple-text-editor’).send_keys('')ActionChains(driver).key_down(Keys.TAB).perform()ActionChains(driver).key_down(Keys.TAB).perform()driver.find_element_by_id(’alloy-simple-text-editor’).click()driver.find_element_by_id(’alloy-simple-text-editor’).send_keys('是')ActionChains(driver).key_down(Keys.TAB).perform()ActionChains(driver).key_down(Keys.TAB).perform()driver.find_element_by_id(’alloy-simple-text-editor’).click()driver.find_element_by_id(’alloy-simple-text-editor’).send_keys('是')ActionChains(driver).key_down(Keys.TAB).perform()ActionChains(driver).key_down(Keys.TAB).perform()ActionChains(driver).key_down(Keys.TAB).perform()driver.find_element_by_id(’alloy-simple-text-editor’).click()driver.find_element_by_id(’alloy-simple-text-editor’).send_keys('否')ActionChains(driver).key_down(Keys.TAB).perform()ActionChains(driver).key_down(Keys.TAB).perform()driver.find_element_by_id(’alloy-simple-text-editor’).click()driver.find_element_by_id(’alloy-simple-text-editor’).send_keys('否')ActionChains(driver).key_down(Keys.TAB).perform()driver.find_element_by_id(’alloy-simple-text-editor’).click()driver.find_element_by_id(’alloy-simple-text-editor’).send_keys('填寫你的地址')driver.find_element_by_id(’alloy-simple-text-editor’).send_keys(Keys.ENTER)time.sleep(1)小結
上面的代碼是根據我所填寫的健康表流程寫的,或許你并不能直接拿過去用,(這也正是我所期望的,哈哈),但是里面的功能實現比較清楚的,這樣你就可以按照你的流程寫代碼了。Good good study, day day up.
到此這篇關于基于python+selenium自動健康打卡的文章就介紹到這了,更多相關selenium自動健康打卡內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: