【自动化】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')