确保跨平台自动化测试脚本的稳定运行:获取谷歌浏览器与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的版本信息,并确保它们之间的兼容性。这对于在不同平台上进行自动化测试至关重要。希望这个分享能对大家有所帮助。如果有任何问题或建议,欢迎在评论区留言讨论,共同提升自动化测试的效率和稳定性。

相关推荐
默_笙4 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_426003964 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫4 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技4 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读4 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时4 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
奇思妙想聪明勤奋的小羊4 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1234 天前
2026年第38周GitHub趋势周报
python·科技·github
IZero074 天前
Jev 与 Laya
python·语言模型