python模拟网页输入与按钮点击——东南大学一键健康上报

文章目录

  • 一、环境安装
  • 二、示例
    • 1.XPath获取
    • 2.代码实现
  • 总结


一、环境安装

安装selenium : 在终端输入: pip install selenium
下载chromedriver :[ [http://chromedriver.storage.googleapis.com/index.html]]下载 (与安装的chrome浏览器版本一致)。解压后放在python.exe同目录下。

二、示例

1.XPath获取

例如使用chrome浏览器:
右键点击输入框或按钮,点击“检查”:
如图:
python模拟网页输入与按钮点击——东南大学一键健康上报_第1张图片
右键点击相应的元素,点击复制完整的XPath路径:
python模拟网页输入与按钮点击——东南大学一键健康上报_第2张图片
获取的XPath在之后代码中使用。

2.代码实现

代码如下:

#coding=utf-8
#####seu一键上报体温####
#######作者Gulzar######
from selenium import webdriver
import time
import re
import random
import sys
temp=random.uniform(35,37)#随机生成体温
str_temp=str(temp)
print(str_temp)
opt = webdriver.ChromeOptions()#创建浏览器
opt.set_headless()#无窗口模式
driver = webdriver.Chrome(options=opt)#创建浏览器对象
print("The browser has been opened.")
driver.get('http://ehall.seu.edu.cn/qljfwapp2/sys/lwReportEpidemicSeu/index.do?t_s=1615173794065&_sec_version_=1&gid_=ZCtaN3VqbnlHQVlIUmNBRG5qN29YV1ZLWU9LY21lN0NRbG1qYndqRkRGTUlwQUV4aWpza3RIQ1Fzb3F6eUhYY1NOdzNiTjFEVC8zekEyQ3dXR0U3RWc9PQ&EMAP_LANG=zh&THEME=indigo#/dailyReport')#打开网页
#driver.maximize_window()#最大化窗口
time.sleep(1)#加载等待
driver.find_element_by_id('username').send_keys("*******") #账号
driver.find_element_by_id('password').send_keys("*******") #密码
driver.find_element_by_xpath("//button[@class='auth_login_btn primary full_width'][@type='submit']").click()#点击按钮
print("Login in completed.")
time.sleep(2)
try:
    driver.find_element_by_xpath("/html/body/main/article/section/div[2]/div[1]").click()
    time.sleep(2)
    driver.find_element_by_xpath("/html/body/div[11]/div/div[1]/section/div[2]/div/div[4]/div[2]/div[1]/div[1]/div/input").send_keys(str_temp)
    driver.find_element_by_xpath("//*[@id='save']").click()
    print("Data has been saved.")
    time.sleep(2)
    driver.find_element_by_xpath("/html/body/div[60]/div[1]/div[1]/div[2]/div[2]/a[1]").click()
    print("Mission complete!")
    time.sleep(1)
except:
    print("Exception occured. Maybe you have already uploaded your health data.")
finally:
    driver.close()
    sys.exit()

用XPath查找到对应的按钮或输入框之后,使用send_keys()函数输入,click()函数点击按钮,按正常的使用流程即可实现自动上报。


总结

可以在控制面板的管理工具界面建立计划任务,之后只需要开着电脑就可以实现每天自动上传体温了。

你可能感兴趣的:(python,selenium,python,chrome)