#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Chrome配置检查脚本 验证32位ChromeDriver是否能正常驱动64位Chrome """ import os import sys import platform import subprocess def check_system_info(): """检查系统信息""" print("🖥️ 系统信息检查") print("=" * 40) # 系统架构 system_arch = platform.architecture()[0] machine = platform.machine() print(f"系统架构: {system_arch}") print(f"处理器: {machine}") # 判断是否64位系统 is_64bit = system_arch == '64bit' or 'AMD64' in machine print(f"64位系统: {'✅ 是' if is_64bit else '❌ 否'}") return is_64bit def check_chrome_installation(): """检查Chrome安装情况""" print("\n🌐 Chrome浏览器检查") print("=" * 40) # Chrome可能的安装路径 chrome_paths = [ r"C:\Program Files\Google\Chrome\Application\chrome.exe", r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", r"C:\Users\{}\AppData\Local\Google\Chrome\Application\chrome.exe".format(os.getenv('USERNAME', '')) ] chrome_found = False chrome_path = None chrome_is_64bit = False for path in chrome_paths: if os.path.exists(path): chrome_found = True chrome_path = path # 如果Chrome在Program Files目录,通常是64位 chrome_is_64bit = "Program Files" in path and "(x86)" not in path print(f"✅ 找到Chrome: {path}") print(f"Chrome位数: {'64位' if chrome_is_64bit else '32位或未确定'}") break if not chrome_found: print("❌ 未找到Chrome浏览器") return False, None, False # 尝试获取Chrome版本 try: result = subprocess.run([chrome_path, '--version'], capture_output=True, text=True, timeout=5) if result.returncode == 0: version = result.stdout.strip() print(f"Chrome版本: {version}") else: print("⚠️ 无法获取Chrome版本") except Exception as e: print(f"⚠️ 获取版本失败: {e}") return True, chrome_path, chrome_is_64bit def check_chromedriver(): """检查ChromeDriver""" print("\n🚗 ChromeDriver检查") print("=" * 40) # 可能的ChromeDriver路径 driver_paths = [ "chromedriver.exe", "downloads/chromedriver.exe", "./chromedriver.exe" ] driver_found = False driver_path = None for path in driver_paths: if os.path.exists(path): driver_found = True driver_path = path print(f"✅ 找到ChromeDriver: {path}") break if not driver_found: print("❌ 未找到ChromeDriver") return False, None # 获取ChromeDriver版本和架构信息 try: result = subprocess.run([driver_path, '--version'], capture_output=True, text=True, timeout=5) if result.returncode == 0: version_info = result.stdout.strip() print(f"ChromeDriver版本: {version_info}") # ChromeDriver通常是32位的,这是正常的 print("📋 说明: ChromeDriver通常是32位程序,这是正常的") else: print("⚠️ 无法获取ChromeDriver版本") except Exception as e: print(f"⚠️ 获取版本失败: {e}") return True, driver_path def test_selenium_connection(): """测试Selenium连接""" print("\n🧪 Selenium连接测试") print("=" * 40) try: from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options print("✅ Selenium模块导入成功") # 查找ChromeDriver driver_paths = ["chromedriver.exe", "downloads/chromedriver.exe", "./chromedriver.exe"] driver_path = None for path in driver_paths: if os.path.exists(path): driver_path = path break if not driver_path: print("❌ 未找到ChromeDriver,跳过连接测试") return False # 配置Chrome选项 chrome_options = Options() chrome_options.add_argument('--headless') # 无头模式,不显示浏览器 chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') # 创建服务 service = Service(driver_path) print("🚀 正在启动Chrome WebDriver...") # 创建WebDriver driver = webdriver.Chrome(service=service, options=chrome_options) # 简单测试 driver.get("data:text/html,