Python + Selenium UI自动化测试:Chrome 115+版本自动获取浏览器版本及驱动更新指南
一、背景说明
Chrome 115+版本后,驱动与浏览器版本必须严格匹配。本文实现以下核心功能:
- 自动获取当前Chrome浏览器版本
- 使用国内镜像下载匹配的chromedriver
- 自动解压并配置驱动路径
- 跨平台支持(Windows/macOS/Linux)
二、环境准备
# 安装必要库
pip install selenium requests beautifulsoup4 platform
三、完整实现代码
import os
import re
import zipfile
import requests
import platform
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.common.exceptions import SessionNotCreatedException
class ChromeDriverAutoUpdater:
def __init__(self):
self.chrome_version = self._get_chrome_version()
self.os_type = self._get_os_type()
self.driver_dir = os.path.dirname(os.path.abspath(__file__))
def _get_os_type(self) -> str:
"""获取操作系统类型和架构"""
system = platform.system().lower()
arch = platform.machine().lower()
if system == "windows":
return "win64" # 统一使用64位版本
elif system == "darwin":
return "mac-arm64" if "arm" in arch else "mac-x64"
elif system == "linux":
return "linux64"
else:
raise OSError("Unsupported operating system")
def _get_chrome_version(self) -> str:
"""自动获取Chrome浏览器版本"""
system = platform.system()
# Windows系统
if system == "Windows":
import winreg
try:
key

最低0.47元/天 解锁文章
2807

被折叠的 条评论
为什么被折叠?



