当前位置:   article > 正文

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

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

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

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

脚本内容

以下是详细的脚本代码:

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

执行结果

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

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
  • 2
  • 3

脚本解读

  1. 获取Google Chrome版本

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

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

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

总结

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/805164
推荐阅读
相关标签
  

闽ICP备14008679号