import os
import shutil
import platform
import re
import winreg
import subprocess
import requests
import urllib
import zipfile
import sys
from alive_progress import alive_bar
def get_chrome_version():
"""
获取本地浏览器版本信息
:return:
"""
system = platform.system()
# 不同的系统,注册表命令不一致
if system == "Linux":
command = "google-chrome --version"
elif system == "Windows":
command = 'reg query "HKLM\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome" /v DisplayVersion'
elif system == "Darwin": # macOS
command = 'mdls -name kMDItemVersion -raw /Applications/Google\ Chrome.app'
else:
print("无法识别此电脑系统")
# 通过注册表来获取谷歌浏览器版本
try:
version_info = subprocess.check_output(command, shell=True, stderr=subprocess.DEVNULL).decode("utf-8")
version = re.search(r"(\d+\.\d+\.\d+\.\d+)", version_info)
if version:
print("Google Chrome 版本:", version.group(1))
return version.group(1)
except (subprocess.CalledProcessError, AttributeError):
print("无法获取 Google Chrome 版本")
raise
def down_load_driver(driver_path):
"""
下载谷歌浏览器驱动
:param driver_path: 存放谷歌驱动的目录路径
:return:
"""
chrome_version = get_chrome_version().split('.')[0] # 获取谷歌浏览器版本号 主版本号即可
# 获取符合主版本号最新的driver驱动版本
drivers_url = "https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json"
res = requests.get(url=drivers_url).text
down_load_driver_version = re.findall((chrome_version + '.\d+.\d+.\d+/'), res)[-1].split('/')[0]
system = platform.system() # 获取操作系统名称
architecture = platform.architecture()[0] # 获取操作系统的位数信息
# 判断操作系统平台信息,
if system == "Windows":
if architecture == "64bit":
system_platform = "win64"
elif architecture == "32bit":
system_platform = "win32"
elif system == "Linux":
system_platform = "linux64"
elif system == "Darwin" or system == "mac":
if architecture == "arm64":
system_platform = "mac-arm64"
elif architecture == "x86_64":
system_platform = "mac-x64"
else:
print("苹果操作系统位数判断不对,请手动调整代码:{}".format({architecture}))
else:
print("无法识别该操作系统,请手动调整代码")
# driver压缩包名称
driver_zipfile_name = "chromedriver-{}.zip".format(system_platform)
driver_url = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/{}/{}/{}".format(
down_load_driver_version, system_platform, driver_zipfile_name)
total_size = int(urllib.request.urlopen(driver_url).headers['Content-Length'])
result = requests.get(driver_url, stream=True)
# driver文件压缩包路径
driver_zipfile_path = os.path.join(driver_path, driver_zipfile_name)
# 开始下载驱动
with open(driver_zipfile_path, 'ab') as f:
with alive_bar(int(total_size / 1024) + 1, bar='bubbles', force_tty=True) as bar:
for i in result.iter_content(chunk_size=1024):
f.write(i)
bar()
# 第一种解压方式 下载完成后解压 解压出所有文件
with zipfile.ZipFile(driver_zipfile_path, 'r') as z_file:
for fileM in z_file.namelist():
z_file.extract(fileM, driver_path)
# 第二种解压方式 单独解压其中的chromedriver文件出来
"""
target_folder = driver_zipfile_name.split('.')[0] # 要解压的文件夹名
target_filename = 'chromedriver.exe' # 要解压的文件名 要注意 Linux、Mac的文件名为 chromedriver windows 为 chromedriver.exe
output_filename = os.path.join(driver_path, target_filename) # 解压后的输出文件名
with zipfile.ZipFile(driver_zipfile_path, 'r') as zip_ref:
for file_info in zip_ref.infolist():
if target_folder in file_info.filename and file_info.filename.endswith(target_filename):
with zip_ref.open(file_info) as input_file, open(output_filename, 'wb') as output_file:
shutil.copyfileobj(input_file, output_file)
"""
print("----------------------【driver文件下载完成】---------------------")
# 删除压缩包
os.remove(driver_zipfile_path)
print("----------------------【删除[{}]压缩包】---------------------".format(driver_zipfile_path))
if __name__ == '__main__':
down_load_driver(r'F:\镜像')
这个Python脚本演示了如何根据不同操作系统自动下载适用于谷歌浏览器的驱动程序。该脚本会检测当前操作系统(Windows、Linux 或 macOS),然后根据操作系统类型选择正确的谷歌浏览器驱动下载链接。这种自动化的方法确保了无论在哪种操作系统上运行脚本,都能获得相应的驱动程序,为Web自动化测试等任务提供便利。在实际应用中,务必提供有效的驱动下载链接以确保脚本的正常运行。
背景:在过去,我们通常通过访问 https://chromedriver.storage.googleapis.com/index.html 这个链接来下载谷歌浏览器驱动程序,以供自动化测试等任务使用。然而,近期发现该链接并不能下载版本号为115及之后的驱动程序。
解决方案: 为了确保自动下载谷歌浏览器驱动程序的顺利进行,这次使用以下Python脚本,它会根据不同的操作系统,自动选择适用的驱动下载链接,并进行下载。
下载驱动链接也同步更新了 ,现在可以下载115及之后的驱动程序。
由于条件有限,Mac和Linux系统还没测试该脚本的可行性,烦请有条件的伙伴帮忙测试一下,将结果在评论区告知于我,不胜感激 ———————————————— 版权声明:本文为CSDN博主「不氪金玩家」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_36969228/article/details/132457556