公司动态
MacOS Python Selenium 自动化操作浏览器插件
MacOS Python Selenium 自动化操作浏览器插件文章目录MacOS Python Selenium 自动化操作浏览器插件前言MacOS Python Selenium 自动化操作插件一、资源下载1.1 chrome-for-testing ChromeDriver二、Selenium调用Chrome 浏览器(指定浏览器工具)三、浏览器缓存-另存为3.1 什么是「浏览器缓存另存为」3.2 应用场景3.3 怎么实现(MacOS)四、Selenium打开指定浏览器缓存数据并跳转至插件前言MacOS Python Selenium 自动化操作插件在区块链和 Web3 自动化测试中浏览器插件往往是关键环节。传统的 Selenium 自动化网页操作已经非常成熟但涉及到插件时尤其在MacOS 系统中操作的复杂度和兼容性问题会显著增加。本篇内容将基于以下环境实现一套完整的插件自动化流程✅MacOS (Apple Silicon)✅Python Selenium✅Chrome for Testing✅插件目标是实现以下功能 加载浏览器缓存保留登录态和插件 启动指定插件环境 自动化操作插件内部页面元素如点击账户名等通过合理配置--user-data-dir、使用正确版本的 ChromeDriver 与 Chrome并结合 Selenium 的自动化控制能力你将可以自动登录插件快速切换账户环境批量运行 Web3 操作或自动化测试脚本这将极大提升 Web3 自动化测试的效率与稳定性 一、资源下载1.1 chrome-for-testing ChromeDriver下载地址https://googlechromelabs.github.io/chrome-for-testing/我的设备是MacOs Apple M2 Pro二、Selenium调用Chrome 浏览器(指定浏览器工具)fromseleniumimportwebdriverfromselenium.webdriver.chrome.serviceimportServicefromselenium.webdriver.chrome.optionsimportOptions# 配置 Chrome 的路径和 ChromeDriver 的路径chrome_driver_path./chromedriver# 你下载并解压的 ChromeDriver 路径chrome_exe_path./chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing# 设置 ChromeOptionschrome_optionsOptions()chrome_options.binary_locationchrome_exe_path# 指定 Chrome 浏览器路径# 创建 WebDriver 服务serviceService(chrome_driver_path)# 启动浏览器driverwebdriver.Chrome(serviceservice,optionschrome_options)# 获取浏览器信息capabilitiesdriver.capabilitiesprint(Browser details:,capabilities)# 打开页面driver.get(https://www.google.com)# 打印页面标题print(driver.title)# 关闭浏览器driver.quit()三、浏览器缓存-另存为3.1 什么是「浏览器缓存另存为」将浏览器的用户数据目录即浏览器配置文件、登录状态、插件、缓存等持久化保存到本地目录中以便下次继续使用。3.2 应用场景自动登录保持、比如你登录了插件关闭浏览器后再次打开仍然保持登录状态多账号配置、你可以为每个账号建一个配置文件目录互不干扰提高效率、避免每次初始化插件或重载缓存节省测试/操作时间3.3 怎么实现(MacOS)/路径/Google Chrome for Testing--user-data-dir/路径/ChromeProfiles/插件数据存储目录名称四、Selenium打开指定浏览器缓存数据并跳转至插件importosimportsubprocessimportsignalimporttimefrom seleniumimportwebdriver from selenium.webdriver.chrome.serviceimportService from selenium.webdriver.chrome.optionsimportOptions# # 一、配置路径需根据实际环境修改# # ChromeDriver 可执行文件路径与 Chrome 版本匹配chrome_driver_path/路径/chromedriver# Chrome 浏览器路径Chrome for Testing 或系统 Chromechrome_exe_path/路径/chrome/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing# 指定缓存目录user-data-dir用于加载已有登录状态、插件、Cookie 等user_data_dir/路径/ChromeProfiles/插件数据存储目录名称# 插件的扩展 ID可在 chrome://extensions 中查看plugin_id插件IDplugin_home_urlfchrome-extension://{plugin_id}/home.html# # 二、清理可能已占用缓存目录的 Chrome 进程# def kill_chrome_using_profile(profile_path): 杀掉当前运行中使用同一 user-data-dir 的 Chrome 进程避免冲突 try:# 查找包含 Google Chrome for Testing 名称的进程resultsubprocess.check_output([pgrep,-fl,Google Chrome for Testing],textTrue)forlineinresult.strip().split(\n):ifprofile_pathinline: pidint(line.strip().split()[0])print(f⚠️ 检测到已占用 user-data-dir 的 Chrome 进程 PID: {pid}正在终止...)os.kill(pid, signal.SIGKILL)except subprocess.CalledProcessError:# 没有匹配进程时不会报错pass kill_chrome_using_profile(user_data_dir)# # 三、配置 Chrome 启动参数# chrome_optionsOptions()chrome_options.binary_locationchrome_exe_path# 设置 Chrome 可执行文件路径chrome_options.add_argument(f--user-data-dir{user_data_dir})# 加载指定缓存目录chrome_options.add_argument(--disable-gpu)chrome_options.add_argument(--no-sandbox)chrome_options.add_argument(--remote-debugging-port9222)# 可选用于调试chrome_options.add_experimental_option(detach, True)# 启动后不自动关闭# # 四、启动浏览器实例# serviceService(chrome_driver_path)driverwebdriver.Chrome(serviceservice,optionschrome_options)# # 五、跳转到插件页面插件主页地址# driver.get(plugin_home_url)# 等待页面加载time.sleep(3)# 不调用 driver.quit() 以便调试时浏览器保持开启、比如点击插件内部某个元素driver.quit()