本文摘自php中文网,作者coldplay.xixi,侵删。

【相关学习推荐:python视频教程】
由于学校要求我们每天都要在官网打卡签到疫情信息,多多少少得花个1分钟操作,程序员的尊严告诉我们坚决不能手动打卡。正巧最近学了selenium,于是画了个5分钟写了个自动打卡签到地小程序。
测试环境:python3.7 , selenium,chrome浏览器
seleium和chromedriver的配置在这里就不讲了,这里放个连接
首先找到学校信息门户的登录页:

1 2 3 4 5 6 | from selenium import webdriver
import time
url = 'http://my.hhu.edu.cn/login.portal'
driver = webdriver.Chrome()
driver.get(url)
|
这时候就该模拟登录了,首先找到用户名的input框。按ctrl+shift+c,打开开发者工具,点击用户名右边的input框,即可在右边的开发者工具中找到input框对应的代码。


右击该模块,点击copy->copy Xpath 。(Xpath是用来定位该input控件位置的)
1 2 3 4 5 6 | root = ''
password = ''
driver.find_element_by_xpath( '//*[@id="username"]' ).send_keys(root)
driver.find_element_by_xpath( '//*[@id="password"]' ).send_keys(password)
|
账号密码输完了,就该点击登陆了。按ctrl+shift+c,点击登录按钮,在右边的开发者工具对应的代码块右键copy->copy xpath,获得button的xpath。
1 2 | driver.find_element_by_xpath( '//*[@id="changeBack"]/tbody/tr/td[2]/table[1]/tbody/tr[2]/td/p/input[1]' ).click()
|

在登陆后的页面中,找到了健康上报的功能框。点击该功能框,发现页面跳转到了签到页面:
复制该页面的网址,让程序在登陆后跳转到该页面:
1 2 | form = 'http://form.hhu.edu.cn/pdc/form/list'
driver.get(form)
|
让程序点击“本科生健康打卡:
1 | driver.find_element_by_xpath( '/html/body/p[1]/p[4]/p/section/section/p/a/p[2]' ).click()
|
会跳转到以下的页面

点击提交,即完成签到
1 | driver.find_element_by_xpath( '//*[@id="saveBtn"]' ).click()
|
完整的程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from selenium import webdriver
import time
root = ''
password = ''
url = 'http://my.hhu.edu.cn/login.portal'
driver = webdriver.Chrome()
driver.get(url)
driver.find_element_by_xpath( '//*[@id="username"]' ).send_keys(root)
driver.find_element_by_xpath( '//*[@id="password"]' ).send_keys(password)
driver.find_element_by_xpath( '//*[@id="changeBack"]/tbody/tr/td[2]/table[1]/tbody/tr[2]/td/p/input[1]' ).click()
form = 'http://form.hhu.edu.cn/pdc/form/list'
driver.get(form)
driver.find_element_by_xpath( '/html/body/p[1]/p[4]/p/section/section/p/a/p[2]' ).click()
driver.find_element_by_xpath( '//*[@id="saveBtn"]' ).click()
|
相关学习推荐:编程视频
以上就是python+selenium实现简易地疫情信息自动打卡签到功能的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python语言语句块的标记是什么?
Python利用format方法保留三位小数
Python问题1:indentationerror:expected an indented block
Python如何换行继续输入
Python为什么适合人工智能
解析Python中executemany和序列用法教程
详解Python 2.6 升级至 Python 2.7 的实践心得
Python中str相关操作讲解
Python如何实现颜色
Python如何实现优先级队列(附代码)
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python+selenium实现简易地疫情信息自动打卡签到功能