确保跨平台自动化测试脚本的稳定运行:获取谷歌浏览器与ChromeDriver版本20240703

确保跨平台自动化测试脚本的稳定运行:获取谷歌浏览器与ChromeDriver版本

在自动化测试中,确保谷歌浏览器(Google Chrome)与ChromeDriver版本匹配至关重要,特别是跨平台(Windows和Linux)测试时。为了简化这一过程并避免版本不兼容问题,我编写了一个Python脚本,帮助在Windows系统下快速获取谷歌浏览器和ChromeDriver的版本信息,从而指导Linux上的部署工作。

脚本内容

以下是详细的脚本代码:

python 复制代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import os

def get_chrome_version():
    options = Options()
    options.add_argument("--headless")
    options.add_argument("--disable-gpu")
    driver = webdriver.Chrome(options=options)
    driver.get("chrome://version")
    version_element = driver.find_element(By.XPATH, '//*[@id="version"]')
    chrome_version = version_element.text.split(' ')[0]
    driver.quit()
    return chrome_version

def find_chromedriver():
    possible_locations = [
        os.path.join(os.environ['LOCALAPPDATA'], 'Google', 'Chrome', 'Application'),
        os.path.join(os.environ['PROGRAMFILES'], 'Google', 'Chrome', 'Application'),
        os.path.join(os.environ['PROGRAMFILES(X86)'], 'Google', 'Chrome', 'Application'),
        "C:\\",  # 在C盘根目录进行广泛搜索
    ]

    for location in possible_locations:
        for root, dirs, files in os.walk(location):
            if 'chromedriver.exe' in files:
                return os.path.join(root, 'chromedriver.exe')
    return None

def get_chromedriver_version(chromedriver_path):
    try:
        version_output = os.popen(f'"{chromedriver_path}" --version').read()
        chromedriver_version = version_output.split(' ')[1]
        return chromedriver_version
    except Exception as e:
        return str(e)

def main():
    try:
        chrome_version = get_chrome_version()
        print(f"Google Chrome Version: {chrome_version}")
    except Exception as e:
        print(f"Google Chrome not found: {e}")

    chromedriver_path = find_chromedriver()
    if chromedriver_path:
        print(f"ChromeDriver found at: {chromedriver_path}")
        try:
            chromedriver_version = get_chromedriver_version(chromedriver_path)
            print(f"ChromeDriver Version: {chromedriver_version}")
        except Exception as e:
            print(f"Failed to get ChromeDriver version: {e}")
    else:
        print("ChromeDriver not found.")

if __name__ == "__main__":
    main()

执行结果

运行该脚本后,输出结果如下:

复制代码
Google Chrome Version: 126.0.6478.127
ChromeDriver found at: C:\Users\math\.cache\selenium\chromedriver\win64\125.0.6422.141\chromedriver.exe
ChromeDriver Version: 125.0.6422.141

脚本解读

  1. 获取Google Chrome版本

    • 使用selenium库创建一个无头(headless)Chrome实例。
    • 访问chrome://version页面,提取并返回Chrome版本信息。
  2. 查找ChromeDriver

    • 定义可能的ChromeDriver安装路径列表,包括本地应用程序数据目录、程序文件目录等。
    • 遍历这些路径,搜索名为chromedriver.exe的文件,并返回其完整路径。
  3. 获取ChromeDriver版本

    • 运行找到的ChromeDriver,并使用命令行获取其版本信息。
    • 处理可能的异常情况,确保脚本稳健运行。

总结

通过这个脚本,我们可以快速获取Google Chrome和ChromeDriver的版本信息,并确保它们之间的兼容性。这对于在不同平台上进行自动化测试至关重要。希望这个分享能对大家有所帮助。如果有任何问题或建议,欢迎在评论区留言讨论,共同提升自动化测试的效率和稳定性。

相关推荐
荣码8 小时前
LangGraph多Agent协作:3个Agent干活比1个强,但我踩了4个坑
java·python
用户8356290780511 天前
Python 操作 PDF 附件:添加、查看与管理指南
后端·python
宇宙之一粟1 天前
乐企版式文件生成平台
java·后端·python
学测绘的小杨2 天前
CompassFusion:一个从 GNSS 到 GNSS/INS 组合导航的独立工程包
python
zzzzzz3102 天前
当产品经理说这个很简单:我用Python自动化处理奇葩需求的实战指南
python·pycharm·产品经理
雪隐2 天前
个人电脑玩AI-06让5060 Ti给你打工——不光能画画,Qwen3-TTS还能学人说话,连我老板都信了!
人工智能·后端·python
兵慌码乱3 天前
面向桌面端的资产管理系统分层架构设计与核心模块实现
python·系统架构·sqlite·pyqt5·数据库设计·桌面应用开发·mvc架构
hboot3 天前
AI工程师第三课 - 机器学习基础
python·scikit-learn·kaggle
顾林海3 天前
Agent入门阶段-编程基础-Python:流程控制
python·agent·ai编程
呱呱复呱呱3 天前
Django CBV 源码解读:一个请求是怎么找到你的 get() 方法的
python·django