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

作者:X.Leibniz 时间:2022-04-28 06:31:28 

每天都要记得健康打卡

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

使用的工具

需要有一定的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.

来源:https://blog.csdn.net/qq_46022736/article/details/112427369

标签:python,selenium,自动
0
投稿

猜你喜欢

  • 超长文章的智能分页-支持HTML

    2008-03-20 13:21:00
  • Python实现的最近最少使用算法

    2022-07-10 22:48:27
  • python基于tkinter制作下班倒计时工具

    2022-09-17 00:43:36
  • python爬取哈尔滨天气信息

    2023-12-10 22:28:05
  • python中使用多线程改进flask案例

    2022-11-07 05:44:55
  • python如何提升爬虫效率

    2021-12-17 22:18:24
  • PHP的mysqli_thread_id()函数讲解

    2023-06-13 10:09:43
  • 中文段首不需要空两格

    2010-04-23 20:31:00
  • 为什么要登录?

    2010-11-30 21:41:00
  • 漂亮的Django Markdown富文本app插件的实现

    2021-08-14 15:23:24
  • MySQL安全大讲堂:MySQL数据库安全配置

    2009-10-18 11:24:00
  • Javascript的时间戳和php的时间戳转换注意事项

    2023-11-19 16:12:30
  • Python实现扣除个人税后的工资计算器示例

    2021-06-02 14:34:39
  • python 公共方法汇总解析

    2023-11-23 03:03:13
  • python开发的自动化运维工具ansible详解

    2022-12-11 08:40:00
  • asp生成不需要数据库的中奖码

    2008-07-18 12:31:00
  • Python中生成器和迭代器的区别详解

    2022-12-04 02:32:08
  • Python OpenCV 彩色与灰度图像的转换实现

    2022-04-14 18:36:59
  • Sql Server 索引使用情况及优化的相关Sql语句分享

    2012-06-06 19:49:36
  • Python爬虫HTPP请求方法有哪些

    2023-07-25 16:55:06
  • asp之家 网络编程 m.aspxhome.com