实用Python定时点击Chrome网页按钮

python 复制代码
# conda create -n python39 python=3.9

# 导入 selenium 和 time 库
from webdriver_manager.chrome import ChromeDriverManager # python -m pip install webdriver-manager
# service = ChromeService(executable_path=ChromeDriverManager().install())
# driver = webdriver.Chrome(service=service)

from selenium import webdriver # python -m pip install selenium
# driver = webdriver.Chrome(executable_path="chromedriver.exe") # 创建一个浏览器对象
# driver = webdriver.Chrome(executable_path="msedgedriver.exe") # 创建一个浏览器对象
from selenium.webdriver.chrome.service import Service

# https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/?form=MA13LH#downloads
service = Service(executable_path="ChromePortable64-110.0.5481.178\chromedriver.exe")
# 连接到127.0.0.1:9222
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9515") #  --remote-debugging-port=9222
#
driver = webdriver.Chrome(service=service, options=options) # 1. 使用驱动实例开启会话
print(driver)

import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10) # 设置显式等待为10秒
original_window = driver.current_window_handle # 存储原始窗口的 ID
print("original_window: ", driver.current_window_handle)
# assert len(driver.window_handles) == 1
# 循环执行,直到找到一个新的窗口句柄
# driver.quit()
for window_handle in driver.window_handles:
    print("window_handle: ", window_handle)
    # 关闭 window_handle
    driver.switch_to.window(window_handle)
    # print(driver.title)
    # driver.title 包含 "在线考试" 
    if "baidu.com" in driver.current_url and "百度" in driver.title:
        # driver.close()
        break
    else:
        # driver.close()
        pass
# exit()  
tingzhi = driver.find_elements(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[6]/div/div/a[2]")[0]
print(tingzhi.text)
print(tingzhi.get_attribute('class'))
# exit()
while True:
    anniu= driver.find_elements(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[6]/div/div/a[1]")[0]
    if(anniu.text == "按钮"):
        if(anniu.get_attribute('class') == ""):
            anniu.click()
    while True:#等待text变为xx
        anniu= driver.find_elements(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[6]/div/div/a[1]")[0]
        if(anniu.text == "按钮"):
            print("开始计时...")
            #运行29分钟
            time.sleep(28 * 60) # 29m
            # time.sleep(10) # 10s
            break
        else:
            print("启动中...")
            time.sleep(3)
            continue   
    tingzhi = driver.find_elements(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[6]/div/div/a[2]")[0]
    if(tingzhi.get_attribute('class') == ""):
        tingzhi.click()
    while True:#等待停止变蓝色
        anniu= driver.find_elements(By.XPATH, "/html/body/div[2]/div[3]/div[2]/div[1]/div[2]/div[1]/div/div[3]/table/tbody/tr[1]/td[6]/div/div/a[1]")[0]
        if(anniu.get_attribute('class') == ""):
            print("停止完成")
            break
        else:
            print("停止中...")
            time.sleep(3)
            continue   
    continue
# driver.quit()

python -m pip install webdriver-manager selenium -y

用Python的好处就是稳定,完全模拟了手动操作,用浏览器控制台的JavaScript总感觉是假死啊。

相关推荐
爱吃苹果的梨叔1 天前
训练集群网络中断怎么办?智算机房的 Console 串口排障方案
网络·python·智能路由器·php
李妍.1 天前
02Numpy基础(上)
开发语言·python
TheBestRucy1 天前
Python 九阳神功之贰:面向对象(下)
开发语言·python
2601_965958461 天前
口腔黏膜脱皮超2周未愈建议及时就医
人工智能·python
AI_小站1 天前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain
微小冷1 天前
在不同空间中展示数据(欧式、球面、庞加莱圆盘)
python·双曲空间·微分几何·geomstats·庞加莱圆盘·球面
felixking1 天前
C++20 协程
开发语言·c++·协程
weixin_431600441 天前
Agent Workflow 学习向:Code 节点,比模板更强的变量加工
后端·python·学习·ai·
讲温控就好了1 天前
从医疗影像到能源存储——芯片冷却温控技术的跨行业赋能
人工智能·python·能源
DeepVisionary1 天前
从 Qwen3.8-27B 把原生多模态、原生 FP8、桌面级 Agent 三件事一次集齐,看开源大模型的“工业化拐点“
python·自动化