【自动化】Python SeleniumUtil 油猴 工具 自动安装用户脚本

【自动化】Python SeleniumUtil 油猴 工具

【自动化】Python SeleniumUtil 工具-CSDN博客【自动化】Python SeleniumUtil 工具。https://blog.csdn.net/G971005287W/article/details/144565691

油猴工具

复制代码
import time

from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from 服务.Selenium.SeleniumUtil import SeleniumUtil


class 油猴:
    @staticmethod
    def 新建用户脚本(driver, 用户脚本地址url):
        # 这个地址提前获取一下
        driver.get('chrome-extension://ooicokiaoaoadbpknjehhcdiejmbppod/options.html#nav=utils')

        # 记录所有已知的窗口句柄(包括原始标签页)
        before_handles = SeleniumUtil.标签页.获取所有已知窗口句柄(driver)

        time.sleep(2)
        js = f"""
            document.getElementById('input_dXRpbHNfdXRpbHM_url').value='{用户脚本地址url}'
            document.getElementById('input_dXRpbHNfdXRpbHNfaV91cmw_bu').click()
            return true
        """
        driver.execute_script(js)
        time.sleep(3)

        # 再次记录所有窗口句柄
        after_handles = SeleniumUtil.标签页.获取所有已知窗口句柄(driver)

        SeleniumUtil.标签页.切换到新窗口句柄(driver, before_handles, after_handles)

        # 获取新标签页的 URL
        new_url = driver.current_url
        driver.get(new_url)

        安装按钮元素 = SeleniumUtil.页面元素.get元素ByName属性(driver, "安装", True, 5)
        SeleniumUtil.页面元素.点击元素(安装按钮元素)
        安装按钮元素 = SeleniumUtil.页面元素.get元素ByName属性(driver, "重新安装", True, 3)
        SeleniumUtil.页面元素.点击元素(安装按钮元素)
        print(f"用户脚本安装成功: {new_url}")





if __name__ == '__main__':
    驱动路径 = 'C:\\chromedriver\\129.0.6656.0\\chromedriver.exe'
    debuggerAddress = 'localhost:4973'
    driver = SeleniumUtil.Driver.初始化驱动通过Debug模式(驱动路径, debuggerAddress)
    油猴.新建用户脚本(driver, 'https://www.xxx.com.cn/aa.user.js')
相关推荐
小二·几秒前
Python Web 开发进阶实战:可验证网络 —— 在 Flask + Vue 中实现去中心化身份(DID)与零知识证明(ZKP)认证
前端·网络·python
阿蒙Amon2 分钟前
C#每日面试题-ValueTuple和Tuple的区别
开发语言·c#
百***78753 分钟前
一步API+GPT-5.2生产级落地指南:架构设计+高可用+成本控制
开发语言·gpt·架构
Vallelonga3 分钟前
Rust 中 extern “C“ 关键字
c语言·开发语言·rust
勇往直前plus4 分钟前
解决:pycharm运行程序时出现Run ‘python tests for XXX.py‘的问题
ide·python·pycharm
Kratzdisteln5 分钟前
【1902】自适应学习系统 - 完整技术方案
java·python·学习
天若有情6737 分钟前
【Python】从0到1实现轻量级接口测试工具:基于Python+FastAPI+Pytest
python·测试工具·fastapi
头发还没掉光光10 分钟前
Linux网络之TCP协议
linux·运维·开发语言·网络·网络协议·tcp/ip
weixin_4624462311 分钟前
用 Python Tornado + Vue3 + ECharts 搭建 Docker 实时监控 WebSocket 仪表盘
python·echarts·tornado
ValhallaCoder11 分钟前
Day49-图论
数据结构·python·算法·图论