Files
shuidrop_gui/check_chrome_config.py

230 lines
7.2 KiB
Python
Raw Permalink Normal View History

#!/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,<html><body><h1>Test Page</h1></body></html>")
title = driver.title
# 获取浏览器信息
user_agent = driver.execute_script("return navigator.userAgent")
driver.quit()
print("✅ Chrome WebDriver连接成功")
print(f"📄 测试页面标题: {title}")
print(f"🔍 浏览器信息: {user_agent}")
# 分析浏览器位数
if "WOW64" in user_agent or "Win64; x64" in user_agent:
print("🎯 确认: 运行的是64位Chrome浏览器")
else:
print("🎯 运行的Chrome浏览器位数: 32位或未确定")
return True
except ImportError:
print("❌ 未安装Selenium模块")
print("💡 安装命令: pip install selenium")
return False
except Exception as e:
print(f"❌ 连接测试失败: {e}")
return False
def main():
"""主函数"""
print("🔍 Chrome配置全面检查")
print("=" * 50)
# 系统信息检查
is_64bit_system = check_system_info()
# Chrome检查
chrome_found, chrome_path, chrome_is_64bit = check_chrome_installation()
# ChromeDriver检查
driver_found, driver_path = check_chromedriver()
# Selenium测试
selenium_ok = test_selenium_connection()
# 总结
print("\n📋 检查总结")
print("=" * 50)
print(f"64位系统: {'' if is_64bit_system else ''}")
print(f"Chrome浏览器: {'' if chrome_found else ''}")
print(f"ChromeDriver: {'' if driver_found else ''}")
print(f"Selenium测试: {'' if selenium_ok else ''}")
print("\n💡 重要说明:")
print("• 32位ChromeDriver可以完美驱动64位Chrome浏览器")
print("• 这是Google官方的标准设计不存在兼容性问题")
print("• ChromeDriver通过网络协议与Chrome通信位数无关")
if chrome_found and driver_found:
print("\n🎉 配置正常可以开始使用Selenium了")
else:
print("\n⚠️ 需要安装缺失的组件")
if not chrome_found:
print("📥 下载Chrome: https://www.google.com/chrome/")
if not driver_found:
print("📥 下载ChromeDriver: https://chromedriver.chromium.org/downloads")
if __name__ == "__main__":
main()
print()
input("按Enter键退出...")