python实现定时访问web并点击按钮任务

发布时间:2024年01月06日
#UTF-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import time
import pyautogui

import schedule


def job():
    print("执行复位任务...")
    # 创建浏览器驱动,使用Chrome浏览器
    options = webdriver.ChromeOptions()
    options.add_argument('--ignore-certificate-errors')
    driver = webdriver.Chrome(options=options)
    # 访问指定URL
    url = 'https://192.168.28.47'
    driver.get(url)
    # 等待页面加载完毕
    wait = WebDriverWait(driver, 5)
    username_input = wait.until(EC.presence_of_element_located((By.NAME, 'username')))
    password_input = wait.until(EC.presence_of_element_located((By.NAME, 'password')))
    # 输入用户名和密码
    username_input.send_keys('admin')
    password_input.send_keys('admin')
    password_input.send_keys(Keys.ENTER)
    pyautogui.press('enter')
    # 点击登录按钮
    # login_button = wait.until(EC.presence_of_element_located((By.ID, "login")))
    # login_button.click()

    # 切换到框架
    driver.switch_to.frame(0)
    page = driver.current_url
    print(page)
    try:
        reset = wait.until(EC.presence_of_element_located((By.ID, "reset1")))
        reset.click()
    except TimeoutException:
        print("Element not found after 10 seconds.")
    # 模拟按下回车键
    pyautogui.press('enter')
    pyautogui.press('enter')
    # 关闭浏览器
    time.sleep(5)
    driver.quit()

# job()
schedule.every(10).minutes.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

文章来源:https://blog.csdn.net/qq_28935153/article/details/135426800
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。