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

相关推荐
IT小小猪12 分钟前
子进程闪黑窗:pythonw 没有控制台,0x08000000 一个参数解决
运维·windows·python·自动化·进程管理
2601_9628857230 分钟前
如何用 Python 计算终极振荡器(Ultimate Oscillator, UOS)?
开发语言·python
Thanks_ks34 分钟前
Python 列表与元组:可变性与不可变性的差异及使用场景选择
数据结构·python·列表·元组·可变对象·不可变对象·可哈希
benchmark_cc2 小时前
一次获取多只股票实时行情:QuantDash `quotes.get(symbols=[...])` 批量快照
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
geovindu2 小时前
python: PCA
开发语言·后端·python·算法
AI模型调用笔记3 小时前
流式 API 的 usage 为 null?用 cURL 看尾帧,别漏空 choices
python
天诰4 小时前
SA-T-06 轨迹数据工程 脏数据检测
人工智能·python·算法·语言模型·ai编程
Java后端的Ai之路5 小时前
Anthropic 官方文章《Building always-on agents》构建指南
开发语言·网络·python
badhope6 小时前
Agent 说发布成功,文章却 404:我补了 4 张回读收据
后端·python·ai编程
xurileidian6 小时前
第 30 篇 · 边玩边学Python · 猜数字小游戏 把判断循环异常串起来
前端·python·算法