基于python+selenium自动健康打卡的实现代码

2021-01-13 0 722

每天都要记得健康打卡

尊敬的老师,我忘了这次的健康打卡,反思的时候我想了很多东西,反省了很多事情,自己也很懊悔,触犯了学校的规定,深刻认识到自己所犯错误的严重性… 卡!那是小学生才有的检讨。作为一个有点懒的人,对于每次的健康打卡,都是做着重复性的填写,这让本人很是头疼,那就找找止疼药吧

使用的工具

需要有一定的python,html基础,和实践能力(毕竟实践出真知,实践能力强,你可以忽略前两个,你是最棒的!):

  • Pycharm ,在pycharm官网里面下载社区版或专业版(没其他的用途推荐用社区版);
  • Python 我使用的是python3.8,在官网下载,可参考python安装,配置好环境变量方便cmd 装包;
  • Selenium在cmd输入 pip install selenium ,分布式自动化测试工具,用于模拟用户在浏览器的行为;
  • Chromedriver 这个是在pycharm里面驱动Chrome浏览器,下载后解压放在Script下面,Chromedriver需要和自己的谷歌浏览器版本相对应

基于python+selenium自动健康打卡的实现代码

置于桌面 将你写好的py文件放在桌面或者你设置开机自启动管理运行该程序就可以了

基于python+selenium自动健康打卡的实现代码 

接下来就是代码了

代码中必要的地方都进行了注释,注释的多的地方就是我停留较久的地方,,,实习自动打卡的功能算是实现了,但还是有一些问题,比如说网络延时,当网页还未加载出来,你就无法定位该html元素,该程序就无法正常执行完操作,可以发一封邮件提醒你补填。欢迎大家共同探讨遇到的问题或者文章中有其他不足之处还望雅正。

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from 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知识不够,切进iframe
driver.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自动健康打卡内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

遇见资源网 Python 基于python+selenium自动健康打卡的实现代码 http://www.ox520.com/27881.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务