基于undetected_chromedriver的Discord Bot Ubuntu与Windows11兼容问题求助
问题背景
用Python开发的Discord Bot在Windows 11上正常运行,但在Ubuntu服务器上执行xvfb-run --server-args="-screen 0 1920x1080x24" python3 AiSupport.py启动时,触发以下错误:
OSError: [Errno 8] Exec format error: '/home/ubuntu/.local/share/undetected_chromedriver/undetected_chromedriver'
核心代码片段:
from bs4 import BeautifulSoup import undetected_chromedriver as uc from selenium.webdriver.common.by import By import discord from discord.ext import commands client = commands.Bot(command_prefix="!", intents=discord.Intents.all()) import time options = uc.ChromeOptions() options.headless = False chrome = uc.Chrome(options=options)
已尝试更新Python及相关模块,但问题未解决。
错误原因
该错误的核心是下载的chromedriver二进制文件与Ubuntu系统的架构/操作系统不兼容——大概率是Windows环境下缓存的Windows版本chromedriver被同步到了Ubuntu,或者自动下载的chromedriver与服务器CPU架构不匹配(比如x86 vs ARM)。
解决步骤
1. 清理旧缓存与依赖
- 删除undetected_chromedriver的缓存目录:
rm -rf /home/ubuntu/.local/share/undetected_chromedriver/ - 彻底卸载当前的undetected-chromedriver包:
pip uninstall -y undetected-chromedriver
2. 安装Ubuntu兼容的Chrome浏览器
undetected_chromedriver依赖系统中的Chrome,需安装对应架构的版本:
# 下载AMD64架构的Chrome(ARM架构请替换为对应版本的下载链接) wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo dpkg -i google-chrome-stable_current_amd64.deb # 修复依赖缺失问题 sudo apt install -f -y
验证Chrome安装成功:
google-chrome --version
3. 重新安装undetected-chromedriver
重新安装会自动适配当前系统下载对应版本的chromedriver:
pip install undetected-chromedriver
若自动匹配失效,可根据Chrome版本手动指定对应版本的undetected-chromedriver包进行安装。
4. 验证chromedriver兼容性
进入缓存目录,检查文件格式是否匹配Ubuntu系统:
cd /home/ubuntu/.local/share/undetected_chromedriver/ file undetected_chromedriver
正常输出应类似:ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, ...,说明是Linux x86-64兼容格式。
5. 补全xvfb及相关依赖
确保运行环境依赖完整:
sudo apt install xvfb libxss1 libappindicator1 libindicator7 -y
特殊处理:ARM架构服务器
若Ubuntu服务器为ARM架构(如AWS Graviton、树莓派),自动下载的chromedriver可能仍为x86版本,需手动适配:
- 从ChromeDriver官方渠道下载对应Chrome版本的ARM64 chromedriver
- 解压后将chromedriver文件放到指定路径(如
/usr/local/bin/chromedriver-arm) - 在代码中手动指定路径:
import undetected_chromedriver as uc options = uc.ChromeOptions() options.headless = False # 手动指定ARM版chromedriver路径 chrome = uc.Chrome(options=options, executable_path='/usr/local/bin/chromedriver-arm')
内容的提问来源于stack exchange,提问作者HelloWorld5

