python 实现 定时、自动打开网页 实现一系列操作
2021/7/14 11:04:52
本文主要是介绍python 实现 定时、自动打开网页 实现一系列操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
auto_web1.py 实现自动打开网页并进行操作,封装为一个函数。schedule_time1.py实现每天定点执行该程序。
主要是用到selenium模块打开浏览器,然后driver.find_element_by_id 、 driver.find_element_by_xpath
函数来定位需要点击的按钮所在位置,第二个程序是定时执行用的,但是运行一段时间后会出现bug。如果在windows10运行,可以直接使用系统自带的定时计划任务。
#@File : auto_web1.py #@Time : 2021/3/29 21:18 #@Author: Ang import time import os from selenium import webdriver from selenium.common.exceptions import NoAlertPresentException import traceback from selenium.webdriver.common.keys import Keys def auto_web_1(): chromedriver = "C:\Program Files\Google\Chrome\Application" os.environ["webdriver.ie.driver"] = chromedriver driver = webdriver.Chrome() # 选择Chrome浏览器 driver.get('http://hrbeu.edu.cn') #输入自己要打开的网站 driver.maximize_window() # 最大化谷歌浏览器 time.sleep(1) # time.sleep(2) # 处理alert弹窗 try: alert1 = driver.switch_to.alert # switch_to.alert点击确认alert except NoAlertPresentException as e: print("no alert") traceback.print_exc() else: at_text1 = alert1.text print("at_text:" + at_text1) time.sleep(1) # driver.find_element_by_link_text('登录').click() # 点击“账户登录” username = "123456789" # 请替换成你的用户名 password = "123456789" # 请替换成你的密码 user_phone = "123456789" user_local = "00" user_teacher = "张三" user_teacher_number = "12345678910" user_thing1 = "xx" user_thing2 = "xx" user_time_begin = "06:05" user_time_end = "21:55" # <input data-v-1c10fd99="" type="text" name="username" placeholder="请输入学号/工号" id="username" class="input1 required"> driver.find_element_by_id('username').click() # 点击用户名输入框 driver.find_element_by_id('username').clear() # 清空输入框 driver.find_element_by_id('username').send_keys(username) # 自动敲入用户名 time.sleep(1) driver.find_element_by_id('password').click() # 点击密码输入框 driver.find_element_by_id('password').clear() # 清空输入框 driver.find_element_by_id('password').send_keys(password) # 自动敲入密码 # 采用class定位登陆按钮 # driver.find_element_by_id('ext-gen29').click() driver.find_element_by_id('login-submit').click() # 点击登录 # driver.find_element_by_class_name('ant-btn').click() # 点击“登录”按钮 # 采用xpath定位登陆按钮, # driver.find_element_by_xpath('//*[@id="root"]/div/div[3]/form/button').click() time.sleep(2) driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[12]/div[2]/div/p').click() # 更多 driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[12]/div[2]/ul/li[11]/a[1]').click() time.sleep(10) windows = driver.window_handles driver.switch_to.window(windows[-1]) driver.find_element_by_id('V1_CTRL5').click() # 点击电话输入框 driver.find_element_by_id('V1_CTRL5').clear() # 清空输入框 driver.find_element_by_id('V1_CTRL5').send_keys(user_phone) # 自动敲入电话 time.sleep(1) driver.find_element_by_id('V1_CTRL6').click() # 点击公寓输入框 driver.find_element_by_id('V1_CTRL6').clear() # 清空输入框 driver.find_element_by_id('V1_CTRL6').send_keys(user_local) # 自动敲入公寓 driver.find_element_by_class_name("selection").click() # 选择导员姓名输入 time.sleep(1) driver.find_element_by_class_name("select2-search__field").send_keys(user_teacher) time.sleep(1) # win32api.keybd_event(17,0,0,0) #ctrl键位码是17 # win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0) driver.find_element_by_class_name("select2-search__field").send_keys(Keys.ENTER) time.sleep(1) # <a class="command_button_content" name="infoplus_action_6355_1" id="infoplus_action_6355_1"><nobr>提交</nobr></a> driver.find_element_by_id('V1_CTRL8').click() # 点击导员电话 driver.find_element_by_id('V1_CTRL8').clear() # 清空输入框 driver.find_element_by_id('V1_CTRL8').send_keys(user_teacher_number) # 自动敲入 time.sleep(1) driver.find_element_by_id('V1_CTRL9').click() # 点击进出校报备输入框 time.sleep(1) driver.find_element_by_id('V1_CTRL11').click() # 点击出校事由及行程安排输入框 driver.find_element_by_id('V1_CTRL11').clear() # 清空输入框 driver.find_element_by_id('V1_CTRL11').send_keys(user_thing1) # 自动敲入 time.sleep(1) driver.find_element_by_id('V1_CTRL18').click() # 点击 出校类别其他 time.sleep(1) driver.find_element_by_id('V1_CTRL20').click() # 点击开始时间 driver.find_element_by_id('V1_CTRL20').clear() # 清空输入框 driver.find_element_by_id('V1_CTRL20').send_keys(user_time_begin) # 自动敲入 time.sleep(1) driver.find_element_by_id('V1_CTRL22').click() # 点击结束时间 driver.find_element_by_id('V1_CTRL22').clear() # 清空输入框 driver.find_element_by_id('V1_CTRL22').send_keys(user_time_end) # 自动敲入 time.sleep(1) driver.find_element_by_id('V1_CTRL6').click() # 点击公寓输入框 driver.find_element_by_id('V1_CTRL26').click() # 点击目的地 driver.find_element_by_id('V1_CTRL26').clear() # 清空输入框 driver.find_element_by_id('V1_CTRL26').send_keys(user_thing2) # 自动敲入 driver.find_element_by_class_name("command_button_content").click() time.sleep(2) driver.find_element_by_xpath('/html/body/div[9]/div/div[2]/button[1]').click() time.sleep(2) driver.find_element_by_xpath('/html/body/div[10]/div/div[2]/button').click() time.sleep(5) driver.quit() print("自动报备已完成") return 0
#schedule_time1.py #!/usr/bin/env python # -*- coding:utf-8 -*- #@Time : 2021/3/30 20:09 #@Author: Ang #@File : schedule_time1.py from auto_web1 import auto_web_1 import datetime import time from apscheduler.schedulers.blocking import BlockingScheduler def func(): now = datetime.datetime.now() now1= datetime.datetime.now().time() time_sub=24+6-now1.hour begin_day=datetime.datetime(2021, 3, 30, 6, 20, 20, 846283) ts = now.strftime('%Y-%m-%d %H:%M:%S') print("自动任务部署运行中,请勿关闭") print('当前时间 :', ts,"距离任务开始还有",begin_day.hour+24-now.hour,"小时") print("该系统已运行",now.day-begin_day.day,"天") def func_autoweb1(): # 耗时2S now = datetime.datetime.now() ts = now.strftime('%Y-%m-%d %H:%M:%S') auto_web_1() print('当前时间:', ts) time.sleep(2) def dojob(): # 创建调度器:BlockingScheduler scheduler = BlockingScheduler() # 添加任务,时间间隔10s scheduler.add_job(func, 'interval', hours=1, id='test_job1') #hours=1 # scheduler.add_job(func_autoweb1, 'interval', seconds=30, id='test_job2') # 添加任务, 定时每天 06:20:20秒执行任务 scheduler.add_job(func_autoweb1, 'cron', day_of_week='0-6', hour=6, minute=20, second=20) scheduler.start() print("Chen 的自动报备程序运行中...") now = datetime.datetime.now() ts = now.strftime('%Y-%m-%d %H:%M:%S') print('开始时间:', ts) dojob()
这篇关于python 实现 定时、自动打开网页 实现一系列操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程