实用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总感觉是假死啊。

相关推荐
tatasix5 分钟前
Go Redis实现排行榜
开发语言·redis·golang
吴冰_hogan8 分钟前
Java虚拟机(JVM)的类加载器与双亲委派机制
java·开发语言·jvm
程序员shen1616119 分钟前
注意⚠️:矩阵系统源码开发/SaaS矩阵系统开源/抖音矩阵开发优势和方向
java·大数据·数据库·python·php
菜鸟xiaowang11 分钟前
Android.bp java_library_static srcs配置
开发语言·python
怎么就重名了13 分钟前
MAC M4安装QT使用国内镜像源在线安装
开发语言·qt
Evand J14 分钟前
一维、线性卡尔曼滤波的例程(MATLAB)
开发语言·matlab
pl002021 分钟前
C++运算符重载实例
开发语言·c++·运算符重载·单目运算符·双目运算符·流运算符
煤泥做不到的!29 分钟前
挑战一个月基本掌握C++(第十二天)了解命名空间,模板,预处理器
开发语言·c++
青春男大31 分钟前
java队列--数据结构
java·开发语言·数据结构·学习·eclipse
Ven%38 分钟前
DeepSpeed的json配置讲解:ds_config_zero3.json
人工智能·python·ubuntu·json·aigc