赞
踩
在自动化测试中,确保谷歌浏览器(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()
运行该脚本后,输出结果如下:
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
获取Google Chrome版本
selenium
库创建一个无头(headless)Chrome实例。chrome://version
页面,提取并返回Chrome版本信息。查找ChromeDriver
chromedriver.exe
的文件,并返回其完整路径。获取ChromeDriver版本
通过这个脚本,我们可以快速获取Google Chrome和ChromeDriver的版本信息,并确保它们之间的兼容性。这对于在不同平台上进行自动化测试至关重要。希望这个分享能对大家有所帮助。如果有任何问题或建议,欢迎在评论区留言讨论,共同提升自动化测试的效率和稳定性。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。